Why does the background service stops while running?

前端 未结 4 1377
别跟我提以往
别跟我提以往 2021-01-23 08:10

I have a background service in my android application.In which a thread listening recent tasks running frequently.My service overrides both onCreate() and onS

相关标签:
4条回答
  • 2021-01-23 08:25

    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.

    0 讨论(0)
  • 2021-01-23 08:29

    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.

    0 讨论(0)
  • 2021-01-23 08:36

    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.

    0 讨论(0)
  • 2021-01-23 08:43

    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).

    0 讨论(0)
提交回复
热议问题