Reprint Source: http://www.androidlab.cn/viewthread.php? Tid = 421 & extra = page % 3D11. After the instance is started, the system sends a Standard Broadcast Action named android. intent. action. BOOT_COMPLETED, which is sent only once.
2. Construct an IntentReceiver class and reconstruct its abstract method onReceiveIntent (Context context, Intent intent) to start the Service you want to start.
3. in AndroidManifest. add <uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED "> </uses-permission> to obtain the BOOT_COMPLETED license, register the previously reconstructed intentcompleer class, and add <action android: name = "android. intent. action. BOOT_COMPLETED "/> to capture this Action.
Example
Xml:
Code
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission>
<Cycler android: name = ". Olympus sreceiver" 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>
Java:
Code
Public class OlympicsReceiver extends IntentReceiver
{
/* Intent source to receive */
Static final String ACTION = "android. intent. action. BOOT_COMPLETED ";
Public void onReceiveIntent (Context context, Intent intent)
{
If (intent. getAction (). equals (ACTION ))
{
Context. startService (new Intent (context,
Olympus service. class), null); // starts the countdown service.
Toast. makeText (context, "Olympus sreminder service has started! ", Toast. LENGTH_LONG). show ();
}
}
} Note: The current IntentReceiver has changed to BroadcastReceiver, and OnReceiveIntent is onReceive. So the java Code here is: (you can also enable Automatic startup of the application at startup) Code
Public class OlympicsReceiver extends BroadcastReceiver
{
/* Intent source to receive */
Static final String ACTION = "android. intent. action. BOOT_COMPLETED ";
Public void onReceive (Context context, Intent intent)
{
If (intent. getAction (). equals (ACTION ))
{
Context. startService (new Intent (context,
Olympus service. class), null); // starts the countdown service.
Toast. makeText (context, "Olympus sreminder service has started! ", Toast. LENGTH_LONG). show ();
// You can add the application code that is automatically started upon startup.
}
}
}