广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用Context.sendBroadcast() 、 Context.sendOrderedBroadcast() 、 Context.sendStickyBroadcast()来实现的。通常一个广播 Intent 可以被订阅了此 Intent 的多个广播接收者所接收(就像真的收音机一样)。
广播(Broadcas)是一种广泛运用的在应用程序之间传输信息的机制 。而 广播接收者(BroadcastReceiver)是对发送出来的广播进行过滤接收并响应的一类组件。
来自普通应用程序,如一个应用程序通知其他应用程序某些数据已经下载完毕。
BroadcastReceiver 自身并不实现图形用户界面,但是当它收到某个通知后, BroadcastReceiver 可以启动Activity 作为响应,或者通过 NotificationMananger 提醒用户,或者启动 Service 等等。
BroadcastReceiver 广泛应用与应用间的交流。官方文档有这么一段话:
If you don't need to send broadcasts across applications, consider using this class with LocalBroadcastManager instead of the more general facilities described below. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if(activeNetInfo == null) {
sLog("无网络状态");
} else {
switch(activeNetInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
sLog("WIFI状态");
break;
default :
sLog("3G/2G状态");
break;
}
}
}
private void sLog(String msg) {
Log.e("NetworkChangeReceiver", msg);
}
}
注意了,该类必须是public的。
public class MainActivity extends Activity {
private MyBroadcastReceiver receiver = new MyBroadcastReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
this.registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
同步广播(也叫“普通广播”,Context.sendBroadcast()):
发送方发出后,几乎同时到达多个广播接收者处,某个接收者不能接收到广播后进行一番处理后传给下一个接收者,并且无法终止广播继续传播。也就是说,每个广播接收者所收到的广播都是一模一样的,没有应用能够对其进行窜改。
有序广播(Context.sendOrderedBroadcast()):
广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,在AndroidManifest.xml的
Intent intent = new Intent();
intent.setAction("com.plusjun.test.hahaha");
intent.putExtra("name", "xiazdong");
MainActivity.this.sendBroadcast(intent);
一个广播就完成了。
Intent intent = new Intent();
intent.setAction("com.plusjun.test.hahaha");
intent.putExtra("name", "xiazdong");
MainActivity.this.sendOrderedBroadcast(intent, null);
这就是有序广播。
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getExtras().getString("name");
Log.i("Recevier", "接收到:"+name);
abortBroadcast(); //Receiver接收到广播后中断广播
}
}
能力有限了,无法解说。找个时间再研究研究。
摘抄官方原文:
public voidsendStickyBroadcast(Intent intent)
Added in API level 1
该方法需要拥有 BROADCAST_STICKY 权限。
Perform a sendBroadcast(Intent) that is "sticky," meaning the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value ofregisterReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same assendBroadcast(Intent).
You must hold the BROADCAST_STICKY permission in order to use this API. If you do not hold that permission,SecurityException will be thrown.
sendOrderedBroadcast(new Intent("com.plusjun.test.hahaha"), "com.plusjun.test");
代码中与上述例子最大的差别在于Activity中发送广播的方法:
sendOrderedBroadcast(new Intent("com.plusjun.test.hahaha"), "com.plusjun.test");
在第二个参数增加了一个字符串。
在发起广播的应用中,需要在AndroidManifest文件中配置自定义的权限:
相应的,接收器所在的应用中必须设置接收权限:
这样,接收器就可以正确接收到广播了。