How to enable a Service to start automatically on android

Source: Internet
Author: User
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.
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.