Reinherd
Reinherd

Reputation: 5506

IntentService is killed when app is stopped

I've this IntentService:

public class ServiceUpdateNewResults extends IntentService{ 

private void setAlarmToCheckUpdates() {
    Calendar calendar = Calendar.getInstance();

    //calendar.add(Calendar.DAY_OF_YEAR, 1); //dema
    //calendar.set(Calendar.HOUR_OF_DAY, 22); //a les 10
    calendar.add(Calendar.SECOND, 20);

    Intent myIntent = new Intent(this.getApplicationContext(), ReceiverCheckUpdates.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent,0);
    AlarmManager alarmManager = (AlarmManager)this.getApplicationContext().getSystemService(this.getApplicationContext().ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}

public ServiceUpdateNewResults() {
    super("ServiceUpdateNewResults");
}

@Override
protected void onHandleIntent(Intent intent) {
    //fem les coses.
    //Toast.makeText(this.getApplicationContext(), "holaa", Toast.LENGTH_LONG).show();
    setAlarmToCheckUpdates();

    Log.d("debugging","hello");
}

}

And this is calling a BroadCastReceiver every 20 seconds, which ends up calling this service, and this is going to happen "forever". (in a future it will be 1 day, not 20 seconds).

This is the Receiver:

public class ReceiverCheckUpdates extends BroadcastReceiver{
Context context;
@Override
public void onReceive(Context context, Intent intent){
    this.context = context;
    Intent service1 = new Intent(context, ServiceUpdateNewResults.class);
    context.startService(service1);

}
}

This is working perfectly, but if I stop the app from Android settings, the service is also stopped. I want to avoid this. I want that if the App is closed, the service should keep working.

Is it possible? Actually, when is a service killed ?

Upvotes: 0

Views: 1918

Answers (2)

Joe Malin
Joe Malin

Reputation: 8641

The IntentService is part of your app. If the system destroys your app, it will destroy the IntentService. You can reduce the chances of this happening by putting the IntentService in a separate process, but you can't stop the system from destroying an IntentService. You can make it highly unlikely that the system will destroy a Service; to do that, you use a "foreground" Service. However, you should avoid doing this unless you really really need to. In addition, you can't have a foreground IntentService, so you'll have to add your own background Handler and HandlerThread.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006724

if I stop the app from Android settings, the service is also stopped

If by "stop the app from Android settings", you mean that you press the Force Stop button, your app will not run again until something manually runs one of your components (e.g., user launches your activity). More specifically in this case, your alarms are unscheduled.

I want to avoid this.

Then do not press the "Force Stop" button.

I want that if the App is closed, the service should keep working.

In any non-"Force Stop" scenario, the alarms will keep firing (at least until the device falls asleep, given your current implementation).

Upvotes: 2

Related Questions