Broadcast is used in Android programs. There are two ways to register a broadcast receiver:
Method 1: register in the AndroidManifest. xml file
<Cycler android: name = "mycycler">
<Intent-filter>
<Action android: name = "com. and. android. intent. action. TEST"/>
<Action android: name = "android.net. wifi. WIFI_STATE_CHANGED"/>
</Intent-filter>
</Cycler>
You have registered your own broadcast receiver to receive two types of broadcasts. The first is a custom action, and the second is a system. When the WIFI status changes. All for testing, just add one.
This registration method is resident. When the application is closed (before it is uninstalled), the program runs automatically by the system call if the broadcast information is sent.
Method 2: dynamically register in the program code
BroadcastReceiver receiver ER = new MyReceiver ();
IntentFilter filter = new IntentFilter ();
Filter. addAction (Intent. ACTION_SCREEN_OFF); // Add the system broadcast here and send it when the screen is off.
Filter. addAction (ACTION_INTENT_TEST );
RegisterReceiver (receiver, filter );
This registration method is not resident, and the broadcast receiver follows the lifecycle of the Activity. Note: remove the register broadcast receiver before the Activity ends.
UnregisterReceiver (receiver );
If you forget to cancel the registration, it will not be retained in the system. That is, when broadcast comes, the program will not process it.
From continuing revolution