編輯:關於Android編程
MainActivity如下:
package cc.testsmsbroadcastreceiver; import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener; import android.os.Bundle; import android.widget.TextView; import android.app.Activity; /** * Demo描述: * 利用BroadcastReceiver實現監聽短信 * * 注意權限: ** * 詳細資料: * http://blog.csdn.net/lfdfhl/article/details/8195400 * */ public class MainActivity extends Activity{ private TextView mTextView; private SMSBroadcastReceiver mSMSBroadcastReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mTextView=(TextView) findViewById(R.id.textView); mSMSBroadcastReceiver=new SMSBroadcastReceiver(); mSMSBroadcastReceiver.setOnReceivedMessageListener(new MessageListener() { @Override public void OnReceived(String message) { mTextView.setText(message); } }); } }
package cc.testsmsbroadcastreceiver; import java.text.SimpleDateFormat; import java.util.Date; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsMessage; /** * 配置廣播接收者: ** * * 注意: ** * *表示: * 設置此廣播接收者的級別為最高 */ public class SMSBroadcastReceiver extends BroadcastReceiver { private static MessageListener mMessageListener; public SMSBroadcastReceiver() { super(); } @Override public void onReceive(Context context, Intent intent) { Object [] pdus= (Object[]) intent.getExtras().get("pdus"); for(Object pdu:pdus){ SmsMessage smsMessage=SmsMessage.createFromPdu((byte [])pdu); String sender=smsMessage.getDisplayOriginatingAddress(); String content=smsMessage.getMessageBody(); long date=smsMessage.getTimestampMillis(); Date timeDate=new Date(date); SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time=simpleDateFormat.format(timeDate); System.out.println("短信來自:"+sender); System.out.println("短信內容:"+content); System.out.println("短信時間:"+time); mMessageListener.OnReceived(content); //如果短信來自5556,不再往下傳遞 if("5556".equals(sender)){ System.out.println(" abort "); abortBroadcast(); } } } // 回調接口 public interface MessageListener { public void OnReceived(String message); } public void setOnReceivedMessageListener(MessageListener messageListener) { this.mMessageListener=messageListener; } }
前言Service作為Android四大組件之一,應用非常廣泛 本文將介紹Service最基礎的知識:Service的生命周期目錄1. Service分類1.1 Serv
導語這裡展示的View估計項目中多半是用不到的,只是用來加深理解的。文章末尾會有全部的代碼,如果想研究可以復制過去直接運行,不需要額外的資源。先看效果:這裡指針是通過手指
微信接入支付流程跟支付寶差不多,這裡介紹一下接入流程以及注意事項。接入流程:1.1、到微信開放平台添加移動應用,申請權限到微信開放平台注冊開發者賬號,並添加應用,申請支付
在Android的實際開發中,我們Android系統本身已經給我們提供了很豐富的UI以及各種實用的控件,例如TextView,Button,ImageView等。用這些