Search code examples
androidservicebroadcastreceiveralarmmanager

Keep service alive after app has been destroyed


I'm developing an app that need to do some check in the server every certain amount of time. The check consist in verify if there are some notification to display. To reach that goal I implemented Service, Alarm Manager and Broadcast Reciever. This is the code that I'm using so far:

public class MainActivity  {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...
        setRecurringAlarm(this);
    }

    /**
     *
     * @param context
     */
    private void setRecurringAlarm(Context context) {
        Calendar updateTime = Calendar.getInstance();

        Intent downloader = new Intent(context, MyStartServiceReceiver.class);
        downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), 60000, pendingIntent);
    }

    ...
}

Receiver class

public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent dailyUpdater = new Intent(context, MyService.class);
    context.startService(dailyUpdater);
    Log.e("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
}

}

Service class

public class MyService extends IntentService {
    public MyService() {
        super("MyServiceName");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.e("MyService", "Service running!");

        // TODO Do the hard work here

        this.sendNotification(this);
    }

    private void sendNotification(Context context) {
        // TODO Manage notifications here
    }
}

Manifest.xml

<!--SERVICE AND BROADCAST RECEIVER-->
    <service
        android:name=".MyService"
        android:exported="false"/>
    <receiver
        android:name=".MyStartServiceReceiver"
        android:process=":remote"/>

The code works fine, the task in the service will be excecuted periodically. The problem is that the service is destroyed when the app is forced to close. I need to keep alive the service, capable to execute the task, even if the user has closed the app, so the user can be updated via notifications. Thank you for your time!


Solution

  • You can't. If the app is forced closed, that means either its crashed (in which case the service has to be stopped as it may no longer work correctly) or the user force closed it in which case the user wants the app to stop- which means the user doesn't want the service to run. Allowing a service to be automatically restarted even if the user stops it would be basically writing malware into the OS.

    In fact, Android went the exact opposite (and correct) way- if the user force stops an app, NOTHING of the app can run until the user runs it again by hand.