How to enable auto-start on Android

Source: Internet
Author: User

Compiling the Android operating system in the simulator can help us implement the auto-start function of the application at startup. Here, we will use a piece of code to fully master the implementation of Android boot auto-start, to help you master this application.

1. Define a BroadcastReceiver

Java code

 
 
  1. public class BootReceiver extends BroadcastReceiver {   
  2. public void onReceive(Context ctx, Intent intent) {   
  3. Log.d("BootReceiver", "system boot completed");   
  4. //start activity   
  5. String action="android.intent.action.MAIN";   
  6. String category="android.intent.category.LAUNCHER";   
  7. Intent myi=new Intent(ctx,CustomDialog.class);   
  8. myi.setAction(action);   
  9. myi.addCategory(category);   
  10. myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  11. ctx.startActivity(myi);   
  12. //start service   
  13. Intent s=new Intent(ctx,MyService.class);   
  14. ctx.startService(s);   
  15. }   
  16. }   
  17. public class BootReceiver extends BroadcastReceiver {  
  18. public void onReceive(Context ctx, Intent intent) {  
  19. Log.d("BootReceiver", "system boot completed");  
  20. //start activity  
  21. String action="android.intent.action.MAIN";  
  22. String category="android.intent.category.LAUNCHER";  
  23. Intent myi=new Intent(ctx,CustomDialog.class);  
  24. myi.setAction(action);  
  25. myi.addCategory(category);  
  26. myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  27. ctx.startActivity(myi);  
  28. //start service  
  29. Intent s=new Intent(ctx,MyService.class);  
  30. ctx.startService(s);  
  31. }  

2. Configure the license for the consumer to allow the system to receive startup messages. In AndroidManifest. xml:

Xml Code

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

3. Configure the consumer er to receive system startup messages. In AndroidManifest. xml

Xml code for Android auto-start

 
 
  1. < receiver android:name=".app.BootReceiver">   
  2. < intent-filter>   
  3. < action android:name="android.intent.action.BOOT_COMPLETED"/>   
  4. < category android:name="android.intent.category.HOME" />   
  5. < /intent-filter>   
  6. < /receiver>   
  7. < receiver android:name=".app.BootReceiver"> 
  8. < intent-filter> 
  9. < action android:name="android.intent.action.BOOT_COMPLETED"/> 
  10. < category android:name="android.intent.category.HOME" /> 
  11. < /intent-filter> 
  12. < /receiver> 

4. Start the simulator. A dialog box is displayed after the system is started.

This section describes how to enable Android boot automatically.

Related Article

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.