Android广播接收机制详细介绍(附短信接收实现)
Android中广播(BroadcastReceiver)的详细讲解.
1.BroadcastReceiver的注册过程:
(1).广播消息发出来后,只有订阅了该广播的对象才会接收发出来的广播消息并做出相应处理。
**(2).**Android广播分为两个方面:广播发送者和广播接收者。Android中的广播使用了观察者模式,基于消息的发布/订阅事件模型。广播接收者通过Binder机制向AMS进行注册,AMS查找符合相应条件的Broadcastreceiver,将广播发送到BroadcastReceiver(一般情况下是Activity)相应的消息循环队列中。消息循环执行拿到此广播,回调BroadcastReceiver中的onReceive()方法。
(3).广播发送者和广播接收者的执行顺序是异步的,发出去的广播不会关心有无接收者的接收,也不确定接收者是什么时候能接收到。
2.BroadcastReceiver的步骤:
(1).注册广播:广播接收者在AMS中注册广播。
(2).发送广播:广播发送者向AMS中发送广播。
(3).接收广播:广播接收者拿到广播后调用onReceive()方法执行。
以下是一个发送短信的例子,代码如下:
BroadcastReceiverHelper类: packagecom.scd.broadcastreceiver.helper; importcom.scd.broadcastreceiver.activity.MainActivity; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.widget.Toast; /** *广播接收 * *@authorscd * */ publicclassBroadcastReceiverHelperextendsBroadcastReceiver{ /**上下文*/ privateMainActivitymActivity=null; publicBroadcastReceiverHelper(MainActivitymContext){ super(); mActivity=mContext; } @Override publicvoidonReceive(Contextcontext,Intentintent){ if(intent.getAction().equals(MainActivity.ACTION_SENDMESSAGE)){ toast("发送成功"); }elseif(intent.getAction() .equals(MainActivity.ACTION_DELIVERMESSAGE)){ toast("接收成功"); } } publicvoidtoast(Stringtext){ Toast.makeText(mActivity,text,Toast.LENGTH_SHORT).show(); } }
MainActivity类:
packagecom.scd.broadcastreceiver.activity; importcom.scd.broadcastreceiver.R; importcom.scd.broadcastreceiver.helper.BroadcastReceiverHelper; importandroid.app.Activity; importandroid.app.PendingIntent; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.os.Bundle; importandroid.telephony.gsm.SmsManager; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.Toast; publicclassMainActivityextendsActivityimplementsOnClickListener{ privateButtonmButton=null; /**发送短信*/ publicstaticStringACTION_SENDMESSAGE="com.scd.broadcastreceiver.MainActivity.SendMessage"; /**接收短信*/ publicstaticStringACTION_DELIVERMESSAGE="com.scd.broadcastreceiver.MainActivity.DeliverMessage"; /**Reciver类*/ privateBroadcastReceiverHelpermHelperS=null; privateBroadcastReceiverHelpermHelperD=null; /**短信管理类*/ privateSmsManagermSmsManager=null; /**电话号码*/ privateStringmTelephone="13607567010"; /**短信内容*/ privateStringmContext="你好,有什么需要帮助的吗?"; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton=(Button)findViewById(R.id.sendBroadcast); mButton.setOnClickListener(this); mSmsManager=SmsManager.getDefault(); //注册广播 registerBroadcast(); } @Override publicvoidonClick(Viewv){ switch(v.getId()){ caseR.id.sendBroadcast:{ //发送广播 sendBroadcastReceiver(mTelephone,mContext); break; } default: break; } } /** *注册广播 */ publicvoidregisterBroadcast(){ //发送 IntentFilterintentFilterS=newIntentFilter(ACTION_SENDMESSAGE); mHelperS=newBroadcastReceiverHelper(MainActivity.this); this.registerReceiver(mHelperS,intentFilterS); //接收 IntentFilterintentFilterD=newIntentFilter(ACTION_DELIVERMESSAGE); mHelperD=newBroadcastReceiverHelper(MainActivity.this); this.registerReceiver(mHelperD,intentFilterD); } /** *发送广播短信发送的过程:发送的时候需要发送广播一次,接收的时候需要发送广播一次 */ publicvoidsendBroadcastReceiver(Stringtelephone,Stringcontent){ if(telephone!=null){ IntentsIntent=newIntent(ACTION_SENDMESSAGE); //短信成功发送后才发送该广播 PendingIntentsentIntent=PendingIntent.getBroadcast( MainActivity.this,0,sIntent,0); IntentdIntent=newIntent(ACTION_DELIVERMESSAGE); //短信接收后才发送该广播 PendingIntentdeliveryIntent=PendingIntent.getBroadcast( MainActivity.this,0,dIntent,0); mSmsManager.sendTextMessage(telephone,null,content,sentIntent, deliveryIntent); //this.sendBroadcast(intent); } } }
注意:需要添加以下权限:
<!--短信权限--> <uses-permissionandroid:name="android.permission.SEND_SMS"> </uses-permission> <uses-permissionandroid:name="android.permission.READ_SMS"> </uses-permission> <uses-permissionandroid:name="android.permission.RECEIVE_SMS"> </uses-permission>