android基础---->Broadcast的使用
在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件。
目录导航:
广播Broadcast的简要说明
一、 广播的种类:标准广播和有序广播
- 标准广播:是一种完全异步执行的广播,在广播发出之后,所有的广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序可言。这种广播的效率会比较高,但同时也意味着它是无法被截断的
- 有序广播:则是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够收到这条广播消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递
二、 注册BroadcastReceiver的两种方式:
- 静态注册:在AndroidManifest.xml中用标签生命注册,并在标签内用标签设置过滤器
<receiver android:name=".NetworkChangeReceiver"> <intent-filter> <action android:name="com.example.linux.broadcasttest.MyBroadcast" /> </intent-filter> </receiver>
- 动态注册:在java代码中调用registerReceiver(BroadcastReceiver receiver, IntentFilter filter)方法
Intent intent = new Intent(); intent.putExtra("username", "linux"); intent.setAction("com.example.linux.broadcasttest.MyBroadcast"); sendBroadcast(intent);
广播Broadcast的准备工作
我们建立一个android项目,结构如下:
一、 在MainActivity中初始化一些信息:
private final static String TAG = "OtherReceiver"; private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; private LocalBroadcastManager localBroadcastManager; private LocalReceiver localReceiver; @Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "main on create"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
二、 创建三个广播接收器,用于广播的测试:它们分别如下
- LocalReceiver本地广播:
package com.example.linux.broadcasttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * Created by Linux on 2016/3/7. */ public class LocalReceiver extends BroadcastReceiver { private final static String TAG = "OtherReceiver"; public LocalReceiver() { Log.i(TAG, "local receiver constructor."); } @Override public void onReceive(Context context, Intent intent) { String username = intent.getStringExtra("username"); Log.i(TAG, "local username: " + username); } }
- OtherReceiver用于测试有序广播:
package com.example.linux.broadcasttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class OtherReceiver extends BroadcastReceiver { private final static String TAG = "OtherReceiver"; public OtherReceiver() { Log.i(TAG, "other receiver constructor."); } @Override public void onReceive(Context context, Intent intent) { String username = intent.getStringExtra("username"); Log.i(TAG, "other username: " + username); abortBroadcast(); } }
- NetworkChangeReceiver用于测试系统广播的使用:
package com.example.linux.broadcasttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * Created by Linux on 2016/3/7. */ public class NetworkChangeReceiver extends BroadcastReceiver { private final static String TAG = "OtherReceiver"; public NetworkChangeReceiver() { Log.i(TAG, "network receiver constructor."); } @Override //Intent是sendBroadcast中的参数 public void onReceive(Context context, Intent intent) { String username = intent.getStringExtra("username"); Log.i(TAG, "network username: " + username); } }
三、 由于广播会涉及到注册,这里我把广播的注册代码贴出来:在AndroidManifest.xml文件中
<receiver android:name=".NetworkChangeReceiver"> <intent-filter> <action android:name="com.example.linux.broadcasttest.MyBroadcast" /> </intent-filter> </receiver> <receiver android:name=".OtherReceiver"> <intent-filter android:priority="100"> <action android:name="com.example.linux.broadcasttest.MyBroadcast" /> </intent-filter> </receiver> <receiver android:name=".LocalReceiver"> <intent-filter> <action android:name="com.example.linux.broadcasttest.MyBroadcast" /> </intent-filter> </receiver>
Broadcast广播的测试
一、 定义方法sendSystemBroad,用于发送系统广播
// 发送系统广播 public void sendSystemBroad(View view) { intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver, intentFilter); }
- 打印的结果:
03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other receiver constructor. 03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other username: linux 03-23 23:03:20.097 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network receiver constructor. 03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network username: linux 03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local receiver constructor. 03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux
二、 定义方法sendBroadCast,用于测试发送标准广播
// 发送标准广播 public void sendBroadCast(View view) { Intent intent = new Intent(); intent.putExtra("username", "linux"); intent.setAction("com.example.linux.broadcasttest.MyBroadcast"); sendBroadcast(intent); }
- 打印的结果:
03-23 23:05:47.957 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network receiver constructor. 03-23 23:05:47.967 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network username: null
三、 定义方法sendOrderBroad,用于测试有序广播
// 发送有序广播 public void sendOrderBroad(View view) { Intent intent = new Intent(); intent.putExtra("username", "linux"); intent.setAction("com.example.linux.broadcasttest.MyBroadcast"); sendOrderedBroadcast(intent, null); }
- 打印的结果:
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other receiver constructor.
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other username: linux
四、 定义方法sendLocalBroad,用于测试本地广播
// 使用本地广播 public void sendLocalBroad(View view) { localBroadcastManager = LocalBroadcastManager.getInstance(this); Intent intent = new Intent(); intent.putExtra("username", "linux"); intent.setAction("com.example.linux.broadcasttest.MyBroadcast"); localBroadcastManager.sendBroadcast(intent); }
- 打印的结果:
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux 03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux 03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux
友情链接
- 广播的测试源代码 访问密码 1e16
作者:
huhx
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。