Devu Soman
Devu Soman

Reputation: 2266

Why does the background service stops while running?

I have a background service in my android application.In which a thread listening recent tasks running frequently.My service overrides both onCreate() and onStartCommand() methods. When i tried to open some applications like Gallery,Camera etc..., the service will be stopped.It calls the onCreate() method only and not onDestroy() or onStartCommand().I tried to override onLowMemory() in this service but it logs nothing.The application saved internally by specifying

 android:installLocation="internalOnly"  

in the manifest file.

Note: This issue noticed on Micromax A54 2.3.5.

Why does the background service stops sometimes? Is there any solution for this issue?

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            if(intent != null){
                //......
            }
        } catch (Throwable e) {
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //......
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}

And start the service like this

Intent intent = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(intent);

Upvotes: 2

Views: 12578

Answers (4)

David Wasser
David Wasser

Reputation: 95636

You have this code in your Service.onStartCommand():

if(intent != null){

You do realize that if Android kills the process hosting your Service, it will create a new process and create a new instance of your Service and restart it, calling onCreate() and then calling onStartCommand() with a null Intent. This is probably what is happening and why you think that onStartCommand() is not being called.

Upvotes: 0

ivan
ivan

Reputation: 379

I met the same issue. On some devices after a while Android kills my service and even startForeground() does not help. And my customer does not like this issue.

I use SharedPreferences to keep the flag whether the service should be running. Also I use AlarmManager to create a kind of watchdog timer. It checks from time to time if the service should be running and restart it.

Creating/dismissing my watchdog timer:

void setServiceWatchdogTimer(boolean set, int timeout)
{
    Intent intent;
    PendingIntent alarmIntent;
    intent = new Intent(); // forms and creates appropriate Intent and pass it to AlarmManager
    intent.setAction(ACTION_WATCHDOG_OF_SERVICE);
    intent.setClass(this, WatchDogServiceReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    if(set)
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, alarmIntent);
    else
        am.cancel(alarmIntent);
}

Receiving and processing the intent from the watchdog timer:

/** this class processes the intent and
 *  checks whether the service should be running
 */
public static class WatchDogServiceReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {

        if(intent.getAction().equals(ACTION_WATCHDOG_OF_SERVICE))
        {
            // check your flag and 
            // restart your service if it's necessary
        }
    }
}

Indeed I use WakefulBroadcastReceiver instead of BroadcastReceiver. I gave you the code with BroadcastReceiver just to simplify it.

Upvotes: 1

Bhavesh Hirpara
Bhavesh Hirpara

Reputation: 22556

Android stops service when memory needs. You can use

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        if(intent != null){
            //......
        }
    } catch (Throwable e) {
    }
    return START_REDELIVER_INTENT;
}

START_REDELIVER_INTENT can be used.if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int).

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93708

Android can stop any service at any time for any reason. A typical reason is low memory (killing the service to give its memory elsewhere), but I've seen at least a few devices that kill them every X hours regardless. There is no way to ensure you always run. The best you can do is have all your activities try to start your service (in case it isn't running), write your service so it can reload needed data, and set yourself as START_STICKY so if it has enough memory the framework will restart you later.

Upvotes: 2

Related Questions