Android 4.0 and later versions receive boot broadcast BOOT_COMPLETED and boot auto-start services

Source: Internet
Author: User

Android 4.0 and later versions receive boot broadcast BOOT_COMPLETED and boot auto-start services

Scenario Summary: the startup notification cannot be obtained when you create a project, not to mention the function of the startup service in the boot notification. N data sources are found, which are different, too many people say that they will not be able to receive the boot notification after 4.0. through various channels, the way to seek help from all parties is still the method that N people say is not feasible, even if Daniel gave an English document, it was okay.

Finally, I found that the Enable notification has been implemented, but I did not find it by my negligence. I popped up a Toast in the notification, but the real machine or simulator requires many boot items when starting the system, as a result, the last Toast Pop-up is very late, but you have not seen it, or the screen has been locked after the boot, the Toast Pop-up after the screen is not visible. You can pop up more than 10 Toast instances in the notification. Test the function and find that the notification is actually started, but it takes a little longer. Some people say that the startup notification is still not available for different models. I have passed three other mobile phone tests.


The following is a reprinted beta version. You can perform a simple test.

In the past few months, I have seen a lot of posts from my friends on CSDN asking about the auto-start service on startup, that is, the BOOT_COMPLETED broadcast issue. Some people say that the function is broadcast, and some say that the function is not broadcast, here, I will personally test and share my summary with you!

Originally, after 3.1, the system's PackageManager added management for applications in the "stopped state". The stopped state and the stop state in the Activity life cycle are totally different, the stopped state in the package manager indicates an application that has never been started after installation or is manually forced to stop by the user. You can go to "Settings"> "application" to view any installed application and see if the "Stop row" button is grayed out. The system adds two flags: FLAG_INCLUDE_STOPPED_PACKAGES and FLAG_EXCLUDE_STOPPED_PACKAGES to identify whether an intent activates an application in the "stopped state. When neither of the two flags is set or both are set, the FLAG_INCLUDE_STOPPED_PACKAGES effect is used. With the new mechanism above, google thinks that adding FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intent will be very Cooooool by default, which can avoid rogue software and viruses and improve efficiency to some extent, as a result, the RECEIVE_BOOT_COMPLETED broadcast will not be connected if the user has not run the application or stops the application in the settings.
All in all: to get the boot broadcast, you must ensure two points: 1) Your application must run once after installation; 2) Your application has not been forcibly stopped




So how can I enable the auto-start service? Source Code directly

1. BootCompletedReceiver. java File

public class BootCompletedReceiver extends BroadcastReceiver {        @Override      public void onReceive(Context context, Intent intent) {          // TODO Auto-generated method stub          Log.d("LibraryTestActivity", "recevie boot completed ... ");          context.startService(new Intent(context, TestService.class));      }  }  


In the configuration file:

     
       
                      
    
   

Note: you must add the permission; otherwise, the broadcast will not be received.

 


3. After the app is installed on your mobile phone and started once, the app will receive the BOOT_COMPLETED broadcast the next time it is started, and the TestService service will be started.


In a test, go to Settings> application, find the application you just installed, and click "Force stop". After you restart the mobile phone, you will not receive the BOOT_COMPLETED broadcast.
If the app is forced to kill the process by some third-party security software, the BOOT_COMPLETED broadcast will not be received after the app is restarted.





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.