I have recently studied how to enable auto-startup. I have found many examples on the Internet. The experiment is successful. I 'd like to share it with you today!
1. manifest. XML is as follows.
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: versioncode = "1" Android: versionname = "1.0" package = "cn. etzmico. Autorun">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Cycler Android: Name = "cn. etzmico. bootreceiver"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Cycler>
<Activity Android: Name = "cn. etzmico. Autorun" Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
</Application>
<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"> </uses-Permission> sets the permission for auto-start.
</Manifest>
2. bootreceiver. Java
Package CN. etzmico;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. util. log;
Public class bootreceiver extends broadcastreceiver {
Public void onreceive (context, intent ){
If (intent. getaction (). Equals ("android. Intent. Action. boot_completed ")){
Log. D ("bootreceiver", "system boot completed ");
// Context, Autorun. Class
Intent newintent = new intent (context, Autorun. Class );
/* Myactivity action defined in androidmanifest. xml */
Newintent. setaction ("android. Intent. Action. Main ");
/* Myactivity category defined in androidmanifest. xml */
Newintent. addcategory ("android. Intent. Category. launcher ");
/*
* If activity is not launched in activity environment, this flag is
* Mandatory to set
*/
Newintent. setflags (intent. flag_activity_new_task); // sets the startup parameter for the activity to be started. This parameter indicates that a new stack is created for the activity at startup.
/* If you want to start a service, follow below Method */
Context. startactivity (newintent );
}
}
}
3. Autorun. Java
Package CN. etzmico;
Import Android. App. activity;
Import Android. OS. Bundle;
Import CN. etzmico. Autorun. R;
Public class Autorun extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
System. Out. println ("successful ");
}
}
It's that simple.