This section learns the special broadcast receivers in the system.
We did not say in the previous sections that when a broadcast receiver registers with the system, the system initiates the process where the broadcast recipient is located when the broadcast sent by the system matches the action of the broadcast that you registered. Unless the user manually stops the process where the broadcast recipient is located. But there is a situation in life, such as: I am playing online games, it is assumed that the game itself in the system low power situation, will automatically save the current game progress, that is, archived. So to speak, this broadcast recipient only needs to start when the user plays the game, other times the user is not needed. If this broadcast recipient has been resident in the system, it is not a waste of system resources. So we need to start the broadcast receivers dynamically and stop the broadcast receivers. However, as you know, the broadcast receiver will start forever if it is registered in the manifest file. So the only way to do this is not to register in the manifest file and register it dynamically in the code.
Speaking so much, it means that you can dynamically register a broadcast receiver with code.
When it comes to registering a broadcast receiver in the code, the Android system stipulates that screen lock screen and unlock, as well as the power changes must be registered in the code, registration in the manifest file is not working. That's because the screen lock screen and unlock, and the power change events happen too often. If you register in the manifest file, it will be very expensive for the system.
Then we'll use the screen unlock and lock screen implementation to register the broadcast receiver in the code
This time we use the service to create a broadcast receiver.
Activity code: Two buttons, one for creating a service, one for stopping the service
public class Mainactivity extends Activity {Intent Intent; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Intent = new Intent (this, screenservice.class); } Start service public void Openscreen (View v) { log.i ("Mainactivity", "Start broadcast receiver service"); StartService (intent); } Close service public void Closescreen (View v) { log.i ("mainactivity", "Turn off broadcast receiver service"); StopService (intent); }}
Then the service code:
public class Screenservice extends Service {private screenreceiver sreceiver; @Overridepublic ibinder Onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnCreate () {//Register broadcast receiver Super.oncreate ();//Create a broadcast receiver Sreceiver = new Screenreceiver ();// Specifies to receive that type of broadcast intentfilter filter = new Intentfilter ();//Specify Off screen and open screen filter.addaction (Intent.action_screen_off); Filter.addaction (intent.action_screen_on);//Registered Broadcast receiver Registerreceiver (sreceiver, filter); LOG.I ("Screenservice", "Broadcast recipient registered"); @Overridepublic void OnDestroy () {//Destroy the broadcast receiver Super.ondestroy (); Unregisterreceiver (Sreceiver); LOG.I ("Screenservice", "broadcast recipient has been destroyed");}}
Register the broadcast recipient in the service's OnCreate method and destroy the broadcast recipient in the Destory method
The code of the final broadcast receiver:
public class Screenreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {// TODO auto-generated method Stubstring action = Intent.getaction (); if (Intent.ACTION_SCREEN_OFF.equals (action)) {// Receive the broadcast LOG.I of the tube screen ("Screenreceiver", "screen off!");} else if (Intent.ACTION_SCREEN_ON.equals (ACTION)) {//Received on-screen broadcast log.i ("Screenreceiver", "screen on!");}}}
In the manifest file, the service needs to be configured and the broadcast recipient does not need to configure
<application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@ String/app_name " android:theme=" @style/apptheme "> <activity android:name=". Mainactivity " android:label=" @string/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <service android:name= " Com.demo.screenactivity.ScreenService "></service> </application>
The results are as follows:
Ok. That's it, you know.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Four components learning broadcastreceiver four