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?
<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" />
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"); } }
You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.
Started. A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. 2. Bound.
You can do this by making your own Interface where you declare for example " isServiceRunning() ". You can then bind your Activity to your Service, run the method isServiceRunning(), the Service will check for itself if it is running or not and returns a boolean to your Activity.
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 your BroadcastReceiver
):
<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:
<receiver>
element was in the <application>
elementBroadcastReceiver
was specified<intent-filter>
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With