Android listens boot and shutdown broadcast radio

First, turn the broadcast monitor

Android system will be issued after completion of startup startup is complete broadcast (android.intent.action.BOOT_COMPLETED), all registered receivers (BroadcastReceiver) receives the start completion broadcast will receive this broadcast.

1, in the AndroidManifest.xml file added granted access to the system power-on event of the application

     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  

2, the power emitted authoring system boot to complete the broadcast receiver. BroadcastReceiver given class inherits from class code is as follows:

public class BootBroadcastReceiver extends BroadcastReceiver {  
  
    private static final String TAG = "BootBroadcastReceiver";   
    private static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";    
    @Override  
    public void onReceive(Context context, Intent intent) {    
        if (intent.getAction().equals(ACTION_BOOT)) { //开机启动完成后,要做的事情 
            Log.i(TAG, "BootBroadcastReceiver onReceive(), Do thing!");  
        }  
    }  
} 

3, registration broadcast receiver AndroidManifest.xml file

<receiver android:name="com.luxcine.luxcine_settings.BootBroadcastReceiver" >
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

Second, the broadcast monitor off
Android system provides broadcast shutdown and startup broadcast corresponding to the broadcast at issue when the system will be shut down.

1, write broadcast receiver is issued when the system will be shut down. BroadcastReceiver given class inherits from class code is as follows:

public class ShutdownBroadcastReceiver extends BroadcastReceiver {  
    private static final String TAG = "ShutdownBroadcastReceiver";       
    private static final String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN";  
      
    @Override  
    public void onReceive(Context context, Intent intent) {  //即将关机时,要做的事情          
        if (intent.getAction().equals(ACTION_SHUTDOWN)) {  
            Log.i(TAG, "ShutdownBroadcastReceiver onReceive(), Do thing!");  
        }  
    }  
} 

2, registration broadcast receiver AndroidManifest.xml file

<receiver android:name="com.luxcine.luxcine_settings.ShutdownBroadcastReceiver" >
            <intent-filter >
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>
Published 184 original articles · won praise 70 · views 370 000 +

Guess you like

Origin blog.csdn.net/qq_31939617/article/details/103251074