Why does the background service stops while running?

前端 未结 4 1382
别跟我提以往
别跟我提以往 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: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).

提交回复
热议问题