Giovanni Mariotti
Giovanni Mariotti

Reputation: 635

Service start at boot that doesn't work correctly

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);
    }

}

Upvotes: 0

Views: 165

Answers (3)

Richard Le Mesurier
Richard Le Mesurier

Reputation: 29762

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.

Upvotes: 0

Akshay Borgave
Akshay Borgave

Reputation: 112

Add the following line in the java file

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

it should work then.

Upvotes: 0

kalin
kalin

Reputation: 3586

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>

Upvotes: 1

Related Questions