How does the ndroid listen to boot broadcast and shutdown broadcast?

Source: Internet
Author: User

Requirement Description: in some cases, our program needs to run automatically after it is started. When the system is about to close, some records can be written to the specified file.

I. Boot broadcast listening:

After the Android system is started, the launch completion broadcast (Android. Intent. Action. boot_completed) will be sent. All receivers registered with the broadcast that receive the launch completion broadcast (broadcastreceiver) will receive the broadcast.

1. Add the permission granted to the application to access system boot events in the androidmanifest. xml file.

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

2. Compile the receiver sent by the system to start the broadcast. The custom class inherits the broadcastreceiver class. The Code is as follows:

Package COM. android. test; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. util. log;/*** Class Name: bootbroadcastreceiver * Function Description: the broadcast receiver sent by the system at startup * # <uses-Permission Android: Name = "android. permission. receive_boot_completed "/> * @ author android_ls */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, intent) {log. I (TAG, "boot this system, bootbroadcastreceiver onreceive ()"); If (intent. getaction (). equals (action_boot) {log. I (TAG, "bootbroadcastreceiver onreceive (), do thing! ");}}}

3. register the broadcast receiver in the androidmanifest. xml file.

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

Ii. disable broadcast listening:

The Android system provides a shutdown broadcast corresponding to the boot broadcast, which is sent when the system is about to close.

1. Compile the broadcast receiver that will be sent when the system is about to close. The custom class inherits the broadcastreceiver class. The Code is as follows:

Package COM. android. test; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. util. log;/*** Class Name: shutdownbroadcastreceiver * function description: receiver * @ author android_ls */public class shutdownbroadcastreceiver extends broadcastreceiver {Private Static final string tag = "shutdownbroadcastreceiver"; Private Static final string action_sh Utdown = "android. intent. action. action_shutdown "; @ override public void onreceive (context, intent) {log. I (TAG, "shut down this system, shutdownbroadcastreceiver onreceive ()"); If (intent. getaction (). equals (action_shutdown) {log. I (TAG, "shutdownbroadcastreceiver onreceive (), do thing! ");}}}

2. register the broadcast receiver in the androidmanifest. xml file.

   <receiver android:name="com.android.test.ShutdownBroadcastReceiver" >            <intent-filter >                <action android:name="android.intent.action.ACTION_SHUTDOWN" />                <category android:name="android.intent.category.HOME" />            </intent-filter>        </receiver>

Note: There are two ways to start and shut down the instance: 1. Restart the instance and start the instance immediately after it is disabled. In this case, all the broadcasts sent when the boot is complete and the system is about to close can be received. 2. Shut down. At this time, the broadcast sent when the system is about to close cannot be received (what I encountered during the test ). Boot, which can be received when the broadcast is started.

 

Http://blog.csdn.net/android_ls/article/details/8605915

How does the ndroid listen to boot broadcast and shutdown broadcast?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.