Android note-auto-start

Source: Internet
Author: User

The Automatic startup function is used in the project, so I learned how to record it as a note.

The procedure is as follows:

1, Principles:

After searching the data, it is found that when Android is started, a system broadcast is sent with the content ACTION_BOOT_COMPLETED, and its String constant is represented as android. intent. action. BOOT_COMPLETED, so we only need to receive this broadcast in our own application, and then start the APP.

2, Write the receiver

Since broadcast receiver is used to receive broadcast, a new class inherited from BroadcastReceiver is created to receive the above system broadcast.

Importandroid. content. context; importandroid. content. intent; // broadcast receiver. public classBootBroadcastReceiver extends BroadcastReceiver {static final Stringaction_boot = "android. intent. action. BOOT_COMPLETED "; @ Override public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals (action_boot) {Intent autoIntent = newIntent (context, LoginActivity. class); autoIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); context. startActivity (autoIntent );}}}


The above code is to receive the broadcast. After judging that the broadcast is started, it will jump to its own application. Here is the starting LoginActivity.

3, Register Broadcast

Register the broadcast in manifest. xml. When the system sends a boot broadcast, it enters the com. baby. activity. BootBroadcastReceiver class, which is the receiver class we just compiled.

 
                     
                                                  
                       
                  
 

4, Declare Permissions

When it comes to boot, you must grant the corresponding permissions to the application. Therefore, add permissions to manifest. xml.

    
     
 


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.