Android: Re-invoke application if task manager kill

前端 未结 5 2150
谎友^
谎友^ 2021-02-09 00:09

Application thread get close if its killed by task manager. Need to re-invoke application as though its killed by other application or task manager. Any idea?

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-09 00:48

    You have to run background service with START_STICKY command. Just extends Service and override onCommand like this :

    @Override
    public int onStartCommand(Intent intent,int flags,int startId) {
        super.onStartCommand(intent, flags, startId);
    
        return START_STICKY;
    }
    

    Like this your Service is restart when it's close (by system or anything else)

    You just have now check on your service (onCreate for example) if application is running or not and launch it again if not. I suppose PackageManager let you check this or simply put a static boolean is_alive to see if your activity is always running.

    Regards Jim

提交回复
热议问题