Trying to start a service on boot on Android
Solution 1
The other answers look good, but I thought I'd wrap everything up into one complete answer.
You need the following in your AndroidManifest.xml
file:
-
In your
<manifest>
element:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
-
In your
<application>
element (be sure to use a fully-qualified [or relative] class name for yourBroadcastReceiver
):<receiver android:name="com.example.MyBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
(you don't need the
android:enabled
,exported
, etc., attributes: the Android defaults are correct)In
MyBroadcastReceiver.java
:package com.example; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent startServiceIntent = new Intent(context, MyService.class); context.startService(startServiceIntent); } }
From the original question:
- it's not clear if the
<receiver>
element was in the<application>
element - it's not clear if the correct fully-qualified (or relative) class name for the
BroadcastReceiver
was specified - there was a typo in the
<intent-filter>
Solution 2
As an additional info: BOOT_COMPLETE is sent to applications before external storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
More details here in section Broadcast Receivers listening for "boot completed"
Solution 3
How to start service on device boot(autorun app, etc.)
For first: since version Android 3.1+ you don't receive BOOT_COMPLETE if user never started your app at least once or user "force closed" application. This was done to prevent malware automatically register service. This security hole was closed in newer versions of Android.
Solution:
Create app with activity. When user run it once app can receive BOOT_COMPLETE broadcast message.
For second: BOOT_COMPLETE is sent before external storage is mounted. If app is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
In this case there is two solution:
- Install your app to internal storage
- Install another small app in internal storage. This app receives BOOT_COMPLETE and run second app on external storage.
If your app already installed in internal storage then code below can help you understand how to start service on device boot.
In Manifest.xml
Permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Register your BOOT_COMPLETED receiver:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Register your service:
<service android:name="org.yourapp.YourCoolService" />
In receiver OnBoot.java:
public class OnBoot extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// Create Intent
Intent serviceIntent = new Intent(context, YourCoolService.class);
// Start service
context.startService(serviceIntent);
}
}
For HTC you maybe need also add in Manifest this code if device don't catch RECEIVE_BOOT_COMPLETED:
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Receiver now look like this:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
How to test BOOT_COMPLETED without restart emulator or real device? It's easy. Try this:
adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED
How to get device id? Get list of connected devices with id's:
adb devices
adb in ADT by default you can find in:
adt-installation-dir/sdk/platform-tools
Enjoy! )
Solution 4
Along with
<action android:name="android.intent.action.BOOT_COMPLETED" />
also use,
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
HTC devices dont seem to catch BOOT_COMPLETED
Solution 5
note that at the beginning of the question, there is a typo mistake:
<action android:name="android.intent.action._BOOT_COMPLETED"/>
instead of :
<action android:name="android.intent.action.BOOT_COMPLETED"/>
one small "_" and all this trouble :)
Alex
Updated on July 26, 2022Comments
-
Alex almost 2 years
I've been trying to start a service when a device boots up on android, but I cannot get it to work. I've looked at a number of links online but none of the code works. Am I forgetting something?
AndroidManifest.xml
<receiver android:name=".StartServiceAtBootReceiver" android:enabled="true" android:exported="false" android:label="StartServiceAtBootReceiver" > <intent-filter> <action android:name="android.intent.action._BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name="com.test.RunService" android:enabled="true" />
BroadcastReceiver
public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent serviceLauncher = new Intent(context, RunService.class); context.startService(serviceLauncher); Log.v("TEST", "Service loaded at start"); } }