Android之BroadcastReceiver
简介:
BroadcastReceiver本质上就是一种全局的监听器,用于监听系统全局的广播消息,因此它可以非常方便地实现系统中不同组件之间的通信。
特点:
1.BroadcastReceiver用于接收程序所发出的Broadcast Intent,与应用程序启动Activity、Service相同;
2.当应用程序发出一个Broadcast Intent之后,所有匹配该Intent的BroadcastReceiver都有可能被启动;
用法:
网上流行的一张图片可以很好的感受到broadcastreceiver的主要用法:
本次我们通过一个小的示例来一起学习BroadcastReceiver:
建立三个广播类,前两个在manifest里面注册,称为静态注册,最后一个没有在manifest里面注册而在代码中注册,称为动态注册,发送广播的时候有显示发送和隐式发送两种区别。
首先我们来看一下三个广播接收器类的定义以及manifest里面的片段内容如下:
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver01 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String msg = intent.getExtras().getString("data"); Toast.makeText(context, "MyReceiver01收到消息:" + msg, Toast.LENGTH_SHORT).show(); } }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver02 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String msg = intent.getExtras().getString("data"); Toast.makeText(context, "MyReceiver02收到消息:" + msg, Toast.LENGTH_SHORT) .show(); } }
public class MyReceiver03 extends BroadcastReceiver { public final static String INTENT_ACTION_MYRECEIVER03 = "com.bear.broadcastreceiver.intent.action.receiver03"; @Override public void onReceive(Context context, Intent intent) { String msg = intent.getExtras().getString("data"); Toast.makeText(context, "MyReceiver03收到消息:" + msg, Toast.LENGTH_SHORT) .show(); // abortBroadcast(); 取消广播,后续receiver将无法接收到消息 } }
<receiver android:name="com.bear.broadcastreceiver.MyReceiver01" ></receiver> <receiver android:name="com.bear.broadcastreceiver.MyReceiver02"> <intent-filter > <action android:name="com.bear.broadcastreceiver.intent.action.receiver02"/> </intent-filter> </receiver>
主界面共有5个按钮用来测试,主界面类的定义如下:
import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener{ private IntentFilter intentFilter; private MyReceiver03 receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupViews(); intentFilter = new IntentFilter(MyReceiver03.INTENT_ACTION_MYRECEIVER03); receiver = new MyReceiver03(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: //显式发送广播 Intent intent = new Intent(MainActivity.this, MyReceiver01.class); intent.putExtra("data", "bear"); sendBroadcast(intent); break; case R.id.button2: //隐式发送广播 Intent intent02 = new Intent("com.bear.broadcastreceiver.intent.action.receiver02"); intent02.putExtra("data", "bear02"); sendBroadcast(intent02); break; case R.id.button3: //动态注册广播 registerReceiver(receiver, intentFilter); break; case R.id.button4: Intent intent03 = new Intent(MyReceiver03.INTENT_ACTION_MYRECEIVER03); intent03.putExtra("data", "bear03"); sendBroadcast(intent03); // sendOrderedBroadcast(intent03, receiverPermission); 按优先级发送广播 break; case R.id.button5: //注销广播 unregisterReceiver(receiver); break; default: break; } } private void setupViews(){ findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); findViewById(R.id.button3).setOnClickListener(this); findViewById(R.id.button4).setOnClickListener(this); findViewById(R.id.button5).setOnClickListener(this); } }
总结:
1. 每次系统广播(Broadcast)事件发生后,系统就会创建对应的BroadcastReceiver实例,并自动触发它的onReceive()方法,onReceive()方法执行完后,BroadcastReceiver的实例就会被销毁;
2.如果BroadcastReceiver的onReceive()方法不能在10秒内执行完成,Android会认为该程序无响应。所以不要在广播接收者的onReceive()方法里执行一些耗时的操作,否则会弹出ANR(Application No Response)的对话框;
3.如果确实需要根据广播来完成一项比较耗时的操作,则可以考虑通过Intent启动一个Service来完成该操作。不应考虑使用新线程去完成耗时的操作,因为BroadcastReceiver本身的生命周期极短,可能出现的情况是子线程可能还没有结束,BroadcastReceiver就已经退出了;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。