如何让自己的app尽量不被系统杀死

时间:2023-03-09 09:01:31
如何让自己的app尽量不被系统杀死

1.

在Service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建

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

2.

在Service的onDestroy()中重启Service.

@Override
public void onDestroy() {
  super.onDestroy();
  Intent localIntent = new Intent();
  localIntent.setClass(this, MyService.class); //销毁时重新启动Service
  this.startService(localIntent);
}

3.

在mf.xml的application的节点中添加android:persistent="true"

这个方法,必须要system app,所以这个基本没用

4.

fork进程的方式做守护,在5.0已经不行了