标签:
1.发送广播,这里以Activity为例,在Activity的onCreate中发送一个广播
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); //这条广播的标志 intent.setAction("broadcast_test"); //携带数据 intent.putExtra("msg", "这是自定义静态广播消息"); //发送广播 sendBroadcast(intent); } }
2.创建广播接收者,新建一个类继承于BroadCastReceiver,实现其中的onReceive方法
public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //获得广播中的数据 String msg = intent.getStringExtra("msg"); //模拟响应 Toast.makeText(context, msg, 1000).show(); } }
3.在AndroidManifest.xml中注册我们定义好的广播
注意:action标签的name一定要与第一步中intent所设置的action一致,才能收到广播
运行
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //动态注册receiver的Intent过滤器和action IntentFilter filter = new IntentFilter(); filter.addAction("broadcast_test"); registerReceiver(new MyReceiver(), filter); Intent intent = new Intent(); //这条广播的标志 intent.setAction("broadcast_test"); //携带数据 intent.putExtra("msg", "这是自定义动态广播消息"); //发送广播 sendBroadcast(intent); } }
可以看到,这里与静态的不同就在于多了IntentFileter的生成和receiver的注册,相当于将在xml中receiver的注册变成用代码来进行注册
2.同样需要有广播接收者,新建一个类继承于BroadCastReceiver,实现其中的onReceive方法
public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //获得广播中的数据 String msg = intent.getStringExtra("msg"); //模拟响应 Toast.makeText(context, msg, 1000).show(); } }
运行
上面演示了如何静态和动态发送自定义广播,下面通过手机飞行模式的开关来演示如何静态和动态接收来自系统的广播
public class MyReceiver extends BroadcastReceiver{ //用来作为标志,判断是开启还是关闭 private static int count = 0; //如果是飞行模式状态变化的广播,就执行操作 @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //每改变一次飞行状态,count就+1 count++; switch (count%2) { //如果余数为1,说明count是奇数,也就是操作--->开启飞行模式 case 1: Toast.makeText(context, "开启飞行模式", 1000).show(); break; //如果余数为0,说明count是偶数,也就是操作--->关闭飞行模式 case 0: Toast.makeText(context, "关闭飞行模式", 1000).show(); break; } } }
2.在AndroidManifest中注册该广播接收者
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.AIRPLANE_MODE"/> </intent-filter> </receiver>
注意这里用的action是android.intent.action.AIRPLANE_MODE,这是由系统提供的当手机飞行状态改变时发出的广播,正是因为此处将MyReceiver注册为接收指定的系统广播,才会在onReceive中接收到这个广播
public class MyReceiver extends BroadcastReceiver{ //用来作为标志,判断是开启还是关闭 private static int count = 0; //如果是飞行模式状态变化的广播,就执行操作 @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //每改变一次飞行状态,count就+1 count++; switch (count%2) { //如果余数为1,说明count是奇数,也就是操作--->开启飞行模式 case 1: Toast.makeText(context, "开启飞行模式", 1000).show(); break; //如果余数为0,说明count是偶数,也就是操作--->关闭飞行模式 case 0: Toast.makeText(context, "关闭飞行模式", 1000).show(); break; } } }
2.在Activity中对它进行注册
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter filter = new IntentFilter(); //这里将飞行模式改变的Action添加到过滤器中 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); registerReceiver(new MyReceiver(), filter); } }
运行
在上面的动态注册自定义广播的例子中,如果你尝试将当前应用程序kill掉,就会发现Logcat报出如下错误:
从报错信息中可以看出,是由于我们是动态注册的自定义广播,所以在Activity被销毁时,广播接收者将不再接收相应的广播,系统要求必须注销该广播
解决方法很简单,只需要在Activity的onDestroy方法中,调用unregisterReceiver(myreceiver);就可以在Activity退出时注销广播:
@Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(myreceiver); }
有序广播
上面所演示的广播都是通过sendBroadcast发送出去的,是无序的广播,有时候广播接收者是有多个的,如果我们需要将它们设定一定的优先级,就需要通过sendOrderedBroadcast()来发送有序广播
下面通过自定义三个广播接收者和一个广播发送者,演示如何使用有序广播:
1.创建三个广播接收者
MyFirstReceiver.java:
public class MyFirstReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String msg = intent.getStringExtra("msg"); Log.d("MyFirstReceiver", msg); //将数据传输给下一个广播接收者 setResultData("This is the Second Msg From MyFirstReceiver"); } }
MySecondReceiver.java:
public class MySecondReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //获得上一个广播接收者传过来的数据 String msg = getResultData(); Log.d("MySecondReceiver", msg); //将数据传输给下一个广播接收者 setResultData("This is the Third Msg From MySecondReceiver"); } }
MyThirdReceiver.java:
public class MyThirdReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //获得上一个广播接收者传过来的数据 String msg = getResultData(); Log.d("MyThirdReceiver", msg); } }
2.在AndroidManifest.xml中注册三个广播接收者:
<receiver android:name=".MyFirstReceiver"> <intent-filter android:priority="300"> <action android:name="OrderBroadcast"/> </intent-filter> </receiver> <receiver android:name=".MySecondReceiver"> <intent-filter android:priority="200"> <action android:name="OrderBroadcast"/> </intent-filter> </receiver> <receiver android:name=".MyThirdReceiver"> <intent-filter android:priority="100"> <action android:name="OrderBroadcast"/> </intent-filter> </receiver>
注意到这里每个接收者都多加了一个属性:priority,这个属性表示有序广播中的优先级,值越高表示优先级越高,那么当广播发出时,优先级最高的便会第一个接收到广播并拦截下来。然后才会继续往优先级低的传递下去。
3.发送有序广播
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent("OrderBroadcast"); intent.putExtra("msg", "This is the First Msg From Activity"); sendOrderedBroadcast(intent, null); } }
运行,查看Logcat打印结果:
public class MainActivity extends Activity { private MyReceiver receiver = new MyReceiver(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this); IntentFilter filter = new IntentFilter(); filter.addAction("broadcasttest"); //注册局部广播 manager.registerReceiver(receiver , filter); Intent intent = new Intent(); intent.setAction("broadcasttest"); //发送广播 manager.sendBroadcast(intent); } }
至此,对于BroadcastReceiver在Android中的使用已经基本了解,我会在以后的博文中用实战demo来演示广播在开发中的应用!整理不易,如果你觉得本文对你有所帮助,请点个赞,如果有哪些地方存在不足,还望指出,共同探讨。
Android四大组件之BroadcastReceiver详细解析
标签:
原文地址:http://blog.csdn.net/it_zjyang/article/details/51499880