标签:android broadcastreceiver 广播
今天回头看了自己之前学习的笔记,小结了一下BroadcastReceiver,供刚入门的朋友复习和参考。
BroadcastReceiver是android系统一个全局的监听器,可实现不同组件之间的通信。
package com.example.yummyhttputil;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class MyBoradcastReceiver extends BroadcastReceiver {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
}
@Override
public IBinder peekService(Context myContext, Intent service) {
// TODO Auto-generated method stub
return super.peekService(myContext, service);
}
}
<receiver android:name=".MyBoradcastReceiver ">
<intent-filter>
<action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver> 配置了以上信息之后,只要是android.intent.action.MY_BROADCAST这个地址的广播,MyReceiver都能够接收的到。注意,这种方式的注册是常驻型的,也就是说当应用关闭后,如果有广播信息传来,MyReceiver也会被系统调用而自动运行。MyBoradcastReceiver receiver = new MyBoradcastReceiver ();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MYBROADCAST_RECEIVER");
registerReceiver(receiver, filter); 注意,registerReceiver是android.content.ContextWrapper类中的方法,Activity和Service都继承了ContextWrapper,所以可以直接调用。在实际应用中,我们在Activity或Service中注册了一个BroadcastReceiver,当这个Activity或Service被销毁时如果没有解除注册,系统会报一个异常,提示我们是否忘记解除注册了。所以,记得在特定的地方执行解除注册操作:@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
public void send(View view) {
Intent intent = new Intent("android.intent.action.MYBROADCAST_RECEIVER");
intent.putExtra("msg", "hello yummylau!");
sendBroadcast(intent);
}
<receiver android:name=".FirstReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name=".SecondReceiver">
<intent-filter android:priority="999">
<action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name=".ThirdReceiver">
<intent-filter android:priority="998">
<action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
三个接收者的<intent-filter>多了一个android:priority属性,并且依次减小。这个属性的范围在-1000到1000,数值越大,优先级越高。
发送有序广播
public void send(View view) {
Intent intent = new Intent("android.intent.action.MYBROADCAST_RECEIVER");
intent.putExtra("msg", "hello yummylau!");
sendOrderedBroadcast(intent, "scott.permission.MY_BROADCAST_PERMISSION");
}
<permission android:protectionLevel="normal"
android:name="scott.permission.MYBROADCAST_RECEIVER_PERMISSION" /> 然后声明这个权限<uses-permission android:name="scott.permission.MYBROADCAST_RECEIVER_PERMISSION" />如果第一个广播需要添加消息给第二个广播,可以在onReceive()中添加
Bundle bundle = new Bundle();
bundle.putString("addString","我是添加的消息");
setResultExtras(bundle);而第二个广播类中取出Bundle bundle = getResultExtras(true);
String add = bundle.getString("addString");取消继续传递广播abortBroadcast();
标签:android broadcastreceiver 广播
原文地址:http://blog.csdn.net/u010794180/article/details/41841899