Go: Android apk boot auto Run

Source: Internet
Author: User

Background knowledge: when Android starts, it emits a system broadcast with the content action_boot_completed,Its string constant is represented as Android.intent.action.BOOT_COMPLETED. just "snap" to this message in the program and start it again .。 Remember, the Android framework says, "Don ' t call me, I'll be back." What we have to do is prepare to receive the message, and the means to achieve it is to achieve a broadcastreceiver.
Code parsing:
1. Interface Activity:SayHello.java
 PackageCom.ghstudio.BootStartDemo; Importandroid.app.Activity; ImportAndroid.os.Bundle; ImportAndroid.widget.TextView;  Public classSayHelloextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); TextView TV=NewTextView ( This); Tv.settext ("Hello." I started! ");      Setcontentview (TV); }  }  
This code is simple, when the activity starts, create a textview and use it to display "Hello." I started! " Words.
2. Receive broadcast message: Bootbroadcastreceiver.java
 PackageCom.ghstudio.BootStartDemo; ImportAndroid.content.BroadcastReceiver; ImportAndroid.content.Context; Importandroid.content.Intent;  Public classBootbroadcastreceiverextendsBroadcastreceiver {Static FinalString ACTION = "Android.intent.action.BOOT_COMPLETED"; @Override Public voidOnReceive (Context context, Intent Intent) {if(Intent.getaction (). Equals (ACTION)) {Intent sayhellointent=NewIntent (Context,sayhello.class);       Sayhellointent.addflags (Intent.flag_activity_new_task);    Context.startactivity (sayhellointent); }   }    }  
The class derives from the Broadcastreceiver, and in the method OnReceive, detects whether the received intent conforms to the boot_completed and, if so, initiates the SayHello activity.
3. configuration file: Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.ghstudio.BootStartDemo"Android:versioncode= "1"Android:versionname= "1.0" > <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity A Ndroid:name= ". SayHello "Android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action . MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-f ilter> </activity> <receiver android:name= ". Bootbroadcastreceiver "> <intent-filter> <action android:name=" Android.intent.action.BOOT_COMPLETED "/&     Gt       </intent-filter> </receiver> </application> <uses-sdk android:minsdkversion= "3"/> <uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission> </ Manifest>
Note that in the bolded part, the node registers a receiver with the system, and the child node Intent-filter represents the receiving Android.intent.action.BOOT_COMPLETED message. Don't forget to configure Android.permission.RECEIVE_BOOT_COMPLETED permissions.


===========================================

1. To do is to listen to the system issued by the broadcast

 Public class extends broadcastreceiver{        publicvoid  onreceive (context context, Intent Intent) {                 if(Intent.ACTION_BOOT_COMPLETED.equals (Intent.getaction ())) {                        start (context);        }}}

2. Add receiver to the Androidmenifest.xml and indicate that the entrance to the program is Florencetest

    <application        android:icon= "@drawable/ic_launcher"        android:label= "@string/app_name ">        <receiver android:name=". Main. Florencetest "                android:exported=" true "                android:process=": Remote ">            < Intent-filter >                        <action android:name= "Android.intent.action.BOOT_COMPLETED"/>                        <category Android:name= "Android.intent.category.HOME"/>            </intent-filter>        </receiver>  </ Application>

This allows the program to run automatically at boot time.

Transferred from: http://blog.csdn.net/wh_19910525/article/details/7921685

Go: Android apk boot auto Run

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.