Android automatically runs the program upon startup

Source: Internet
Author: User

Sometimes, the application needs to run automatically when it is started, for example, a background service that automatically updates content from the Internet. How can I enable applications that run automatically upon startup? At the time of writing this article, I think that Mr. Gao hentang uses "don't call me, I'll call you back !" To sum up the android framework, let's talk about the idea. Understanding the meaning of this sentence can solve many problems related to the implementation of certain functions on the Android platform.

Scenario: automatically run after the mobile phone is turned onProgram, "Hello. I started!" is displayed on the screen! .

Background: 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. You only need to "capture" the message in the program and start it again. Remember, the android framework says: Don't call me, I'll call you back. What we need to do is to prepare for receiving this message, and the Implementation means is to implement a broadcastreceiver.

CodeResolution:

1. Interface Activity: sayhello. Java

[Java] View plainCopyPrint?

  1. PackageCom. ghstudio. bootstartdemo;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. OS. Bundle;
  4. ImportAndroid. widget. textview;
  5. Public ClassSayhelloExtendsActivity {
  6. @ Override
  7. Public VoidOncreate (bundle savedinstancestate ){
  8. Super. Oncreate (savedinstancestate );
  9. Textview TV =NewTextview (This);
  10. TV. settext ("Hello. I started! ");
  11. Setcontentview (TV );
  12. }
  13. }

Package COM. ghstudio. bootstartdemo; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; </P> <p> public class sayhello extends activity {</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> textview TV = new textview (this); <br/> TV. settext ("hello. I started! "); </P> <p> setcontentview (TV); <br/>}</P> <p>

 

This code is very simple. When the activity starts, create a textview and use it to display "Hello. I started! .

2. Receive broadcast messages: bootbroadcastreceiver. Java

[Java] View plainCopyPrint?

  1. PackageCom. ghstudio. bootstartdemo;
  2. ImportAndroid. content. broadcastreceiver;
  3. ImportAndroid. content. context;
  4. ImportAndroid. content. intent;
  5. Public ClassBootbroadcastreceiverExtendsBroadcastreceiver {
  6.  Static FinalString action ="Android. Intent. Action. boot_completed";
  7.  @ Override
  8.  Public VoidOnreceive (context, intent ){
  9. If(Intent. getaction (). Equals (Action )){
  10. Intent sayhellointent =NewIntent (context, sayhello.Class);
  11. Sayhellointent. addflags (intent. flag_activity_new_task );
  12. Context. startactivity (sayhellointent );
  13. }
  14. }
  15. }

Package COM. ghstudio. bootstartdemo; </P> <p> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; </P> <p> public class bootbroadcastreceiver extends broadcastreceiver {</P> <p> static final string action = "android. intent. action. boot_completed "; </P> <p> @ override <br/> Public void onreceive (context, intent) {</P> <p> If (intent. getaction (). equals (Action) {<br/> intent sayhellointent = new intent (context, sayhello. class); <br/> sayhellointent. addflags (intent. flag_activity_new_task); </P> <p> context. startactivity (sayhellointent); <br/>}</P> <p>

 

This class is derived from broadcastreceiver. In the overwriting method onreceive, it detects whether the received intent meets boot_completed. If yes, it starts the activity sayhello.

3. configuration file: androidmanifest. xml

[XHTML] View plainCopyPrint?

  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Manifest Xmlns: Android=Http://schemas.android.com/apk/res/android"
  3. Package="Com. ghstudio. bootstartdemo"
  4. Android: versioncode="1"
  5. Android: versionname="1.0">
  6. <Application Android: icon="@ Drawable/icon" Android: Label="@ String/app_name">
  7. <Activity Android: Name=". Sayhello"
  8. Android: Label="@ String/app_name">
  9. <Intent-Filter>
  10. <Action Android: Name="Android. Intent. Action. Main" />
  11. <Category Android: Name="Android. Intent. Category. launcher" />
  12. </Intent-Filter>
  13. </Activity>
  14. <Cycler Android: Name=". Bootbroadcastreceiver">
  15. <Intent-Filter>
  16. <Action Android: Name="Android. Intent. Action. boot_completed" />
  17. </Intent-Filter>
  18. </Cycler>
  19. </Application>
  20. <Uses-SDK Android: minsdkversion="3" />
  21. <Uses-Permission Android: Name="Android. Permission. receive_boot_completed"></Uses-Permission>
  22. </Manifest>

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. ghstudio. bootstartdemo "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". sayhello "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <Cycler Android: Name = ". bootbroadcastreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. boot_completed "/> <br/> </intent-filter> <br/> </javaser> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "3"/> </P> <p> <uses-Permission Android: Name = "android. permission. receive_boot_completed "> </uses-Permission> </P> <p> </manifest> <br/>

 

Note that the node registers a receiver with the system. The intent-filter sub-node receives the Android. Intent. Action. boot_completed message. Do not forget to configure the permission for Android. Permission. receive_boot_completed.

Compile the APK package and install it to the simulator or mobile phone. Shut down and reboot.

Run:

 

Extended thinking: In most cases, it is not an interface program that runs automatically, but a service that runs in the background. In this case, you must use startservice to start the corresponding service.

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.