Base class for code that will receive intents sent by sendBroadcast().
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.
You can either dynamically register an instance of this class with Context.registerReceiver()
or statically publish an implementation through the <receiver>
tag in your AndroidManifest.xml
.
Note: If registering a receiver in your Activity.onResume()
implementation, you should unregister it in Activity.onPause()
. (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister inActivity.onSaveInstanceState()
, because this won't be called if the user moves back in the history stack.
注册的两种方式:
1.代码动态注册。
在Activity.onResume()
中注册,在Activity.onPause()解出注册。不要再Activity.onSaveInstanceState()中解出注册,因为他不会被调用在用户返回时。
private MyReceiver4 myReceiver4;
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter("abc");
registerReceiver(myReceiver4, intentFilter);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(myReceiver4);
}
2.AndroidManifest.xml清单文件里 <receiver>
下声明。
<receiver android:name="com.example.broadcast_receiver.MyReceiver" >
<intent-filter android:priority="995">
<action android:name="abc" />
</intent-filter>
</receiver>
android启动activity是链式的,A->B ,B->C ,想将A的消息传给C,就启动广播。
比如电池的变化,发送广播,注册广播接收者。
广播接收者的顺序是无序的。
发送广播有两种方式:
1.normal broadcast 无序的
sendBroadcast(intent);
2.ordered broadcast 有序的
sendOrderedBroadcast(intent, receiverPermission);
There are two major classes of broadcasts that can be received:
- Normal broadcasts (sent with
Context.sendBroadcast
) are completely asynchronous. All receivers of the broadcast are run in an undefined order, often at the same time. This is more efficient, but means that receivers cannot use the result or abort APIs included here. - 无序的异步传送。所有的接收者的启动时是无序的。这很高效,但是不能使用结果或者终止。
- Ordered broadcasts (sent with
Context.sendOrderedBroadcast
) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with theandroid:priority
attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order. - 有序的,一次传一个。让一个接收者返回时,可以传送结果到下一个receiver,或者完全的终止广播,使其不再继续传下去。接收者启动的顺序可以用 <intent-filter>
android:priority来控制。
android:priority相同的话,随机。用abortBroadcast()终止传播。
生命周期
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent)
. Once your code returns from this function, the system considers the object to be finished and no longer active
就是执行 onReceive(Context, Intent)这段时间。
This has important repercussions to what you can do in an
onReceive(Context, Intent)
implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.
当函数返回后,生命周期结束。所以如果执行异步操作,可能在异步操作返回前就结束了。因此,不能再onReceive(Context, Intent)里执行异步操作。
In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use theNotificationManager
API. For the latter, you can use Context.startService()
to send a command to the service.
尤其是,你不可能在BroadcastReceiver里显示dialog或者绑定service。前面的情况(异步),用通知实现。后面的情况(service),用startservice()开启任务。