进程保活大法

收集整理android 进程保活方法

1像素Activity

注册监听屏幕开启和屏幕关闭时的广播,当屏幕关闭时,开启1像素的 Activity, 当屏幕开启时,关闭1像素的 Activity.

  • 查看进程等级

    可以在studio 终端进行命令行, pid 进程id

    1
    2
    3
    adb shell
    su
    cat proc/{pid}/oom_adj

    OnePxActivity

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    public class OnePxActivity extends AppCompatActivity {

    public static void launch(Context context){
    Intent intent = new Intent(context,OnePxActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i("qq","OnPxActivity onCreate.........");
    Window window = getWindow();
    window.setGravity(Gravity.START | Gravity.TOP);

    WindowManager.LayoutParams params = window.getAttributes();
    params.x = 0;
    params.y = 0;
    params.width = 1;
    params.height = 1;
    window.setAttributes(params);

    KeepAliveManager.getInstance().setKeepAliveManager(this);
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    Log.i("qq","OnPxActivity onDestroy.........");
    }
    }

    广播 : 监听开启/关闭的广播

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class KeepAliveReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

    if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
    //屏幕点亮
    Log.i("qq","收到屏幕开启广播");
    KeepAliveManager.getInstance().finishOnePxActivity(context);

    }else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
    //屏幕熄灭, 启动activity
    Log.i("qq","收到屏幕关闭广播");
    KeepAliveManager.getInstance().startOnePxActivity(context);
    }
    }
    }

    辅助类:KeepAliveManager

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    public class KeepAliveManager {

    private KeepAliveManager(){}
    private static KeepAliveManager mInstance = new KeepAliveManager();

    private WeakReference<OnePxActivity> mReference;

    public void setKeepAliveManager(OnePxActivity activity){
    this.mReference = new WeakReference<>(activity);
    }
    public static KeepAliveManager getInstance(){
    return mInstance;
    }

    public void startOnePxActivity(Context context){
    OnePxActivity.launch(context);
    }

    public void finishOnePxActivity(Context context){
    if(null != mReference && mReference.get() != null){
    mReference.get().finish();
    }
    }
    private KeepAliveReceiver mReceiver;
    public void registerReceiver(Context context){
    this.mReceiver = new KeepAliveReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_SCREEN_ON);
    context.registerReceiver(mReceiver,filter);
    }

    public void unregisterReceiver(Context context){
    if(null != mReceiver){
    context.unregisterReceiver(mReceiver);
    }
    }
    }

    MainActivity 使用:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //第一种方式
    KeepAliveManager.getInstance().registerReceiver(this);
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();

    KeepAliveManager.getInstance().unregisterReceiver(this);
    }
    }

亲身实践了,发现确实oom_adj 变小,被杀死的概率比较低了,当屏幕变暗的时候。

前台服务

ForegroundService.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class ForgroundService extends Service {

private final int SERVICE_ID = 1;
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(Build.VERSION.SDK_INT < 18){
//设置成前台服务,并且不显示通知栏消息
startForeground(SERVICE_ID,new Notification());
}else if(Build.VERSION.SDK_INT < 26){
startForeground(SERVICE_ID,new Notification());
startService(new Intent(this,InnerService.class));
}else{//android 8.0

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(manager != null){
NotificationChannel channel = new NotificationChannel("channel","name",NotificationManager.IMPORTANCE_NONE);
manager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channel");
//设置成前台服务,Android9.0 会有通知栏消息,需要添加新的权限
//<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
startForeground(SERVICE_ID,builder.build());
}
}

return super.onStartCommand(intent, flags, startId);
}

class InnerService extends Service{

@Override
public IBinder onBind(Intent intent) {

return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(SERVICE_ID,new Notification());
stopForeground(true);
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
}
}

双进程守护

后续补上…

-------------本文结束感谢您的阅读-------------