To enable boot in the Android system, it takes only a few steps.
1. Define Broadcasting
2. register the broadcast class in Manifest. xml
3. Add Permissions
The following is the specific operation.
First, let's define the broadcast class.
Create a class BootReceiver to inherit BroadcastReceiver.
Rewrite some necessary Java Functions
[Html] 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 context, Intent intent ){
If (intent. getAction (). equals ("android. intent. action. BOOT_COMPLETED ")){
Log. d ("BootReceiver", "system boot completed ");
// Context, AutoRun. class
Intent newnewIntent = 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 );
/* If you want to start a service, follow below method */
Context. startActivity (newIntent );
}
}
}
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 context, Intent 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 );
/* If you want to start a service, follow below method */
Context. startActivity (newIntent );
}
}
}
AutoRun. class is the Activity where the program runs.
Next, register the broadcast class in Manifest. xml.
[Html] <er 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>
<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>
Finally, add the permission.
[Html] <uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission>
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission>
In this way, the Android system is automatically started upon startup. Do not forget the operations in Manifest. xml!
There are two resources. They are essentially the same. After the program runs, shut down the virtual machine or mobile devices, and restart the system. When the system program loads foreign currency, our program runs.
If you want to hide it, you only need to add finish. I have added a System. out in the program, so you don't have to worry about the success of the finish operation. You just need to check whether Successful exists in LogCat.
Yes.
Remember to check whether the time is consistent to avoid unnecessary trouble!
Demo resource 1: http://www.bkjia.com/uploadfile/2012/0308/20120308012933938.zip
Demo Resource 2: http://www.bkjia.com/uploadfile/2012/0308/20120308013159656.zip
From Etzmico