I have read several examples on the Internet and started a system to run it directly.ActivitySmallProgram
CodePaste it below:
FirstBroadcastreceiver
A new class is derived to listen to broadcast messages sent after the system is started.Android. Intent. Action. boot_completed
.
Bootreceiver. Java:
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. util. log;
Public ClassBootreceiverExtendsBroadcastreceiver {
Public VoidOnreceive (context, intent ){
If (Intent. getaction (). Equals ( " Android. Intent. Action. boot_completed " ))
{
Log. D ( " Bootreceiver " , " System Boot completed " );
Intent newintent = New Intent (context, firstrun. Class );
Newintent. setaction ( " Android. Intent. Action. Main " ); // Myactivity action defined in androidmanifest. xml
Newintent. addcategory ( " Android. Intent. Category. Launcher " ); // Myactivity category defined in androidmanifest. xml
Newintent. setflags (intent. flag_activity_new_task ); // If activity is not launched in activity environment, this flag is mandatory to set
Context. startactivity (newintent );
//If you want to start a service, follow below method:
/****************************************** *************
Intent service = new intent (yourservice. action_start );
Service. setclass (context, yourservice. Class );
Context. startservice (service );
**************************************** **************/
}
}
}
Next, this class listens to the activity that we want to run after the system is started.
Firstrun. Java
Import Android. App. activity;
Import Android. OS. Bundle;
Public ClassFirstrunExtendsActivity {
Public VoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
}
Of course, we also need to modify the configuration file. Note that you need to add
<Uses-Permission Android: Name="Android. Permission. receive_boot_completed"></Uses-Permission>
Manifest. xml
<? XML version = "1.0" encoding = "UTF-8" ?>
< Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Package = "Com. Service. prac"
Android: versioncode = "1"
Android: versionname = "1.0" >
< Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
< Cycler Android: Name = ". 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 = ". Firstrun" >
< Intent-Filter >
< Action Android: Name = "Android. Intent. Action. Main" />
< Category Android: Name = "Android. Intent. Category. launcher" />
</ Intent-Filter >
</ Activity >
</ Application >
< Uses-SDK Android: minsdkversion = "3" />
< Uses-Permission Android: Name = "Android. Permission. receive_boot_completed" > </ Uses-Permission >
</ Manifest >