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
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
element:
In your
element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver
):
(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:
element was in the
elementBroadcastReceiver
was specified