标签:
在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件。
目录导航:
一、 广播的种类:标准广播和有序广播
二、 注册BroadcastReceiver的两种方式:
<receiver android:name=".NetworkChangeReceiver"> <intent-filter> <action android:name="com.example.linux.broadcasttest.MyBroadcast" /> </intent-filter> </receiver>
Intent intent = new Intent(); intent.putExtra("username", "linux"); intent.setAction("com.example.linux.broadcasttest.MyBroadcast"); sendBroadcast(intent);
我们建立一个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); }
二、 创建三个广播接收器,用于广播的测试:它们分别如下
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); } }
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(); } }
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>
一、 定义方法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
标签:
原文地址:http://www.cnblogs.com/huhx/p/baseBroadcast.html