标签:代码 span size cas ide strong 异步 state track
Android系统的4个组件最终还剩一种组件了BroadcastReceiver,这个组件是全局监听器,能够监听系统全局的广播消息,能够方便的实现系统中不同组件之间的通信
BroadcastReceiver有自己的进程,系统级监听器,仅仅要存在与之匹配的Intent被广播出来,BroadcastReceiver就会被激发
要创建自己的BroadcastReceiver对象,我们须要继承android.content.BroadcastReceiver,并实现其onReceive方法
MyReceiver.java
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收到的Intent的Action为:" + intent.getAction() + "\n消息内容是:" + intent.getStringExtra("msg") , Toast.LENGTH_LONG).show(); } }Manifest.xml清单文件配置的receiver
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="org.crazyit.action.CRAZY_BROADCAST" /> </intent-filter> </receiver>就是说不管哪个组件中,intent的Action是"org.crazyit.action.CRAZY_BROADCAST" 并使用使用sendBroadcast(intent)发出广播,那么MyReceiver就会被启动
public class BroadcastMain extends Activity { Button send; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取程序界面中的button send = (Button) findViewById(R.id.send); send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 创建Intent对象 Intent intent = new Intent(); // 设置Intent的Action属性 intent.setAction("org.crazyit.action.CRAZY_BROADCAST"); intent.putExtra("msg", "简单的消息"); // 发送广播 sendBroadcast(intent); } }); } }
注冊Receiver有两种方法:
静态注冊
静态注冊是在AndroidManifest.xml文件里配置的。我们就来为MyReceiver注冊一个广播地址:
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.MY_BROADCAST"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
动态注冊
动态注冊须要在代码中动态的指定广播地址并注冊,通常我们是在Activity或Service注冊一个广播,以下我们就来看一下注冊的代码:
MyReceiver receiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.MY_BROADCAST"); registerReceiver(receiver, filter); //注冊<pre name="code" class="java">receiver 和filter普通广播
普通广播对于多个接收者来说是全然异步的。通常每一个接收者都无需等待即能够接收到广播,接收者相互之间不会有影响。
对于这样的广播。接收者无法终止广播。即无法阻止其它接收者的接收动作。
上面的样例就是发送的普通广播
有序广播
有序广播比較特殊,它每次仅仅发送到优先级较高的接收者那里,然后由优先级高的接受者再传播到优先级低的接收者那里。优先级高的接收者有能力终止这个广播。
比如:优先级A>B>C,Broadcast先传给A。再传给B,在传给C。优先级别声明<Intent-filter>元素的android:priority中。数越大级别越高,取值范围在-1000~1000
优先收到Broadcast的接受者能够通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,然后传给下一个接受者。通过Bundle bunde=getResultExtras(true)柯获得上一个接受者存入的数据
public class SortedBroadcast extends Activity { Button send; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取程序中的sendbutton send = (Button) findViewById(R.id.send); send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 创建Intent对象 Intent intent = new Intent(); intent.setAction("org.crazyit.action.CRAZY_BROADCAST"); intent.putExtra("msg", "简单的消息"); // 发送有序广播 sendOrderedBroadcast(intent, null); } }); } }MyReceiver.java
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收到的Intent的Action为:" + intent.getAction() + "\n消息内容是:" + intent.getStringExtra("msg") , Toast.LENGTH_LONG).show(); // 创建一个Bundle对象,并存入数据 Bundle bundle = new Bundle(); bundle.putString("first", "第一个BroadcastReceiver存入的消息"); // 将bundle放入结果中 setResultExtras(bundle); // 取消Broadcast的继续传播 // abortBroadcast(); //① } }MyReceiver2.java
public class MyReceiver2 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = getResultExtras(true); // 解析前一个BroadcastReceiver所存入的key为first的消息 String first = bundle.getString("first"); Toast.makeText(context, "第一个Broadcast存入的消息为:" + first, Toast.LENGTH_LONG).show(); } }清单文件
<receiver android:name=".MyReceiver"> <intent-filter android:priority="20"> <action android:name="org.crazyit.action.CRAZY_BROADCAST" /> </intent-filter> </receiver> <receiver android:name=".MyReceiver2"> <intent-filter android:priority="0"> <action android:name="org.crazyit.action.CRAZY_BROADCAST" /> </intent-filter> </receiver>
Android中BroadcastReceiver组件具体解释
标签:代码 span size cas ide strong 异步 state track
原文地址:http://www.cnblogs.com/wzzkaifa/p/7297861.html