If you need to automatically start the application when Android starts up, you can respond to android. intent. action. BOOT_COMPLETED broadcasts messages. Android is triggered when the android system is started. intent. action. BOOT_COMPLETED message. The procedure is as follows:
1. Define a Broadcast Receiver, such as BootupReceiver.
[Java]
Public class BootupReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
// Better delay some time.
Try {
Thread. sleep (2000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Intent I = new Intent (context, BootupDemoActivity. class );
I. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (I );
}
}
Public class BootupReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
// Better delay some time.
Try {
Thread. sleep (2000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Intent I = new Intent (context, BootupDemoActivity. class );
I. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (I );
}
}
It is used to respond to android. intent. action. BOOT_COMPLETED.
2. Define the Broadcast Receiver in the Manifest File
<Cycler android: name = ". BootupReceiver" android: enabled = "true"
Android: permission = "android. permission. RECEIVE_BOOT_COMPLETED">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"> </action>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Cycler>
3. Add the required permissions.
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>
However, the app will automatically run when the mobile phone carries out its re-revelation. However, your application may be locked by the screen. After you unlock the screen, you can see the application you are running.
If you want to see your application on the machine, you can use the code to remove the screen lock. The method is as follows:
1. Add the following code to the onCreate event processing of the Activity.
[Java]
KeyguardManager keyguardManager
= (KeyguardManager) getSystemService (KEYGUARD_SERVICE );
KeyguardLock lock = keyguardManager. newKeyguardLock (KEYGUARD_SERVICE );
Lock. disableKeyguard ();
KeyguardManager keyguardManager
= (KeyguardManager) getSystemService (KEYGUARD_SERVICE );
KeyguardLock lock = keyguardManager. newKeyguardLock (KEYGUARD_SERVICE );
Lock. disableKeyguard ();
2. Add the required permissions to the Manifest file.
<Uses-permission android: name = "android. permission. DISABLE_KEYGUARD"/>
Download this sample code: http://www.bkjia.com/uploadfile/2012/0518/20120518113514147.zip
Excerpted from the mobile app