我们都知道,Android的广播注册有两种方式,分别为静态和动态注册,动态注册具有灵活性,可随用户需要注销,更大的遵循用户的意愿,在API文档中,提到:
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 in Activity.onSaveInstanceState()
, because this won't be called if the user moves back in the history stack.
即声明了动态注册的一个该注意的地方,假设广播接收器在onResume()中注册,则应该在onPause()中注销,且不要在Activity.onSaveInstanceState()中注销广播,后面这句才是重点,因为这个方法在用户从历史任务(最近打开)中启动应用时不会被调用,这个note应该时常记住,避免不必要的bug出现;
而静态注册则能获得持久性的功能,例如天气预报的默认更新功能、例如现在市面上一部分启动器(Launcher)用于重绘icon等等,静态注册能帮我们做到一些有趣的事情,但是一个广播一直保持在激活状态,到底是好事还是坏事?经过n次的接收、处理广播之后,是否会变得占内存、消耗资源?
The receiver
tag declares an BroadcastReceiver
class that is available as part of the package's application components, allowing the application to receive actions or data broadcast by other applications even if it is not currently running.
Zero or more intent-filter
tags can be included inside of a receiver, to specify the Intents it will receive. If none are specified, the receiver will only be run when an Intent is broadcast that is directed at its specific class name. The receiver tag appears as a child tag of the application
tag.
public abstract void onReceive (Context context, Intent intent)
This method is called when the BroadcastReceiver is receiving an Intent broadcast. During this time you can use the other methods on BroadcastReceiver to view/modify the current result values. This method is always called within the main thread of its process, unless you explicitly asked for it to be scheduled on a different thread usingregisterReceiver(BroadcastReceiver, IntentFilter, String, android.os.Handler)
. When it runs on the main thread you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed). You cannot launch a popup dialog in your implementation of onReceive().
If this BroadcastReceiver was launched through a <receiver> tag, then the object is no longer alive after returning from this function. This means you should not perform any operations that return a result to you asynchronously -- in particular, for interacting with services, you should use startService(Intent)
instead of bindService(Intent, ServiceConnection, int)
. If you wish to interact with a service that is already running, you can use peekService(Context, Intent)
.
The Intent filters used in registerReceiver(BroadcastReceiver, IntentFilter)
and in application manifests are not guaranteed to be exclusive. They are hints to the operating system about how to find suitable recipients. It is possible for senders to force delivery to specific recipients, bypassing filter resolution. For this reason, onReceive()
implementations should respond only to known actions, ignoring any unexpected Intents that they may receive.
Parameters
context | The Context in which the receiver is running. |
---|---|
intent | The Intent being received. |