1 , principle :
when the Android system finishes the BOOT phase, a broadcast called action_boot_completed is sent and we can Broadcastreceiver capture This broadcast, then start our Activity or Service, and of course note that our application You must have permission to capture the broadcast, see the following steps:
The first step is to have an activity or Servicefor Start-up, which is explained by the simplest activity created by the system itself.
Package com.billhoo.study;
Import android.app.Activity;
Import android.os.Bundle;
Public class boottestactivity extends Activity {
/** called when the activity is first created. * /
@Override
Public void onCreate (Bundle savedinstancestate) {
Super. OnCreate (savedinstancestate);
Setcontentview (R.layout.main);
}
}
Brother Two steps: We are going to write a broadcastreceiver to capture action_boot_completed This broadcast and start the Activitywe want to start after capturing.
Package com.billhoo.study;
Import android.content.BroadcastReceiver;
Import android.content.Context;
Import android.content.Intent;
Public class bootcompletedreceiver extends broadcastreceiver {
@Override
public   void   onreceive (context context, intent intent) {
if (Intent.getaction (). Equals (intent.action_boot_completed))
{
Intent newintent = new Intent (context, boottestactivity. Class);
newintent.addflags (Intent.flag_activity_new_task); Note that this tag must be added or the start will fail
Context.startactivity (newintent);
// Note: If it is the boot will see the program to use startactivity, If you want to boot a service to use StartService
}
}
}
Step Three: Register our broadcastreceiver in the androidmanifest.xml configuration file
<?xml version="1.0" encoding= " utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
Package ="Com.billhoo.study" android:versioncode= " 1"
Android:versionname= "1.0" >
<uses-sdk android:minsdkversion= "4" />
<!-- Note One: You must add permission permissions --
<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<!--Activities--
<activity android:name= ". Boottestactivity " android:label=" @string/app_name ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN" />
<category android:name= "Android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Note Two: Add receivers Content- -
<receiver android:name= ". Bootcompletedreceiver ">
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
From