Search code examples
androidbroadcastreceiverintentserviceandroid-intentservice

Android IntentService runs only ONCE after phone is restarted. Why?


Background and issue

I have checked dozens of tutorial, examples and questions here on stackoverflow which are related to the issue that services don't get registered after the phone is switched off.

My issue is almost similar with a little difference: I use an IntentService (I need to collect data from an external database and show it as a notification) and the service runs without any problem every 30 seconds until I switch the phone off.

The interesting part

Here comes the weird behaviour! I turn my phone back and the IntentService is registered ONLY ONCE. After booting up, I get my notification (in the example i use only logs for the sake of simplicity) only once, then never again.

part of Activity code (where I can set the service)

private void setRecurringAlarm(Context context) {

        AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, BackgroundDataServiceReceiver.class);
        PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
                PendingIntent.FLAG_CANCEL_CURRENT);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 30);   

        service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        cal.getTimeInMillis(), 30*1000, pending);   

    }

IntentService

public class BackgroundDataService extends IntentService {

....
@Override
    protected void onHandleIntent(Intent intent) {

      Log.i("BACKGROUNDDATASERVICE STATUS", "running");

    }
}

BroadcastReceiver

public class BackgroundDataServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent dailyUpdater = new Intent(context, BackgroundDataService.class);
        context.startService(dailyUpdater);

    }
}

Manifest

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

 <application
...
  <service android:name="com.example.blgui3.BackgroundDataService" >
        </service>

        <receiver android:name="com.example.blgui3.BackgroundDataServiceReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

...
</application>

As far as I know if my service needs to do background tasks e.g. fetch data from external database with AsyncTask then it is recommended to use IntentService. Even if I launch the app after the boot, the service still runs only once, so it simply does not register the BOOT_COMPLETE action. After struggling for hours with this I have absolutely no clue where I go wrong.


Solution

  • The service is started after boot with BOOT_COMPLETED but that does not start any activity and it seems it is only activity that sets the alarm.