0

I would like to know why the Service does not start when the device completes the boot. I wish that when the device boots, the Service starts, but this does not happen because to start it I have to open the application first.

Here is my code so far:

public class BootService extends BroadcastReceiver{

    Intent intent;

    @Override
    public void onReceive(Context context, Intent intent2) {
        intent = new Intent(context, MyService.class);
        context.startService(intent);
    }

}
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
Giovanni Mariotti
  • 615
  • 1
  • 7
  • 11

3 Answers3

1

Is BootService called?

For BootService to be triggered when boot completes you need to add in your manifest:

1)

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2)

 <receiver android:name="BootService" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
kalin
  • 3,546
  • 2
  • 25
  • 31
  • then add intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this applies also if call activity from a receiver – kalin Aug 05 '13 at 08:46
0

Add the following line in the java file

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

it should work then.

Akshay Borgave
  • 112
  • 2
  • 15
0

For your application to receive the android.intent.action.BOOT_COMPLETED intent, you need to first run an Activity on the device.

Try making a dummy activity, for config, or an introduction to the app.


Other than that, the idea of putting log statements into the BroadcastReceiver and into the Service is a good one.

Also confirm that you have followed djodjo's suggestions, as these are both correct too.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255