Android 进程保活(二)双服务进程保活

Android 进程保活(二)双服务进程保活

八月 06, 2019

1、双进程守护保活方案

创建aidl接口文件用于进程间通信,如下:

方法自定义。

创建本地服务及远程服务 LocalService RemoteService

清单配置如下:

本地服务代码:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.baweigame.processliveapplication;
import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
/**
* 本地服务Service
* 服务是不可见的,一般情况下是运行在后台
*/
public class LocalService extends Service {
private IMyAidlInterface remoteService;
private MyBinder myBinder;
public LocalService() {
}
ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.d("zzzz", "onServiceConnected: localservice is connected...");
try {
// remoteService = (IMyAidlInterface) binder;
// remoteService.getData("localservice");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("zzzz", "onServiceDisconnected: localservice is disconnected...");
startService(new Intent(LocalService.this,RemoteService.class));
bindService(new Intent(LocalService.this,RemoteService.class),serviceConnection, Context.BIND_IMPORTANT);
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("zzzz", "onStartCommand: localservice is onstartcommand...");
startForeground(1,new Notification());
bindService(new Intent(this,RemoteService.class),serviceConnection, Context.BIND_IMPORTANT);
startDo();
return START_STICKY;
}
private void startDo() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(0);
handler.postDelayed(this,2*1000);
}
},2*1000);
}
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d("zzzz", "handleMessage: ****************************************");
}
};
@Override
public IBinder onBind(Intent intent) {
myBinder = new MyBinder();
return myBinder;
}
private class MyBinder extends IMyAidlInterface.Stub{
@Override
public String getData(String name) throws RemoteException {
return LocalService.class.getSimpleName();
}
}
}

远程服务代码文件:

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
50
51
52
53
54
package com.baweigame.processliveapplication;
import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
/**
* 远程服务
*/
public class RemoteService extends Service {
private MyBinder myBinder;
public RemoteService() {
}
ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("zzzz", "onServiceConnected: remoteservice is connected...");
// try {
// IMyAidlInterface localService= (IMyAidlInterface) service;
// localService.getData(RemoteService.class.getSimpleName());
// } catch (RemoteException e) {
// e.printStackTrace();
// }
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("zzzz", "onServiceDisconnected: remoteservice is disconnected...");
startService(new Intent(RemoteService.this,LocalService.class));
bindService(new Intent(RemoteService.this,LocalService.class),serviceConnection,Context.BIND_IMPORTANT);
}
};
@Override
public IBinder onBind(Intent intent) {
myBinder = new MyBinder();
return myBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("zzzz", "onStartCommand: remoteservice is onstartcommand...");
startForeground(1,new Notification());
bindService(new Intent(this,LocalService.class),serviceConnection, Context.BIND_IMPORTANT);
return START_STICKY;
}
private class MyBinder extends IMyAidlInterface.Stub{
@Override
public String getData(String name) throws RemoteException {
return RemoteService.class.getSimpleName();
}
}
}

MainActivity中启动服务

使用静态注册广播接收系统重启或者屏幕解锁等系统广播来启动本地服务增加存活概率
清单文件:

广播文件:

使用电源锁保证息屏后服务不被杀

清单文件中加入权限:

Android 进程保活系列:

Android 进程保活(一)写在前面
Android 进程保活(二)双服务进程包活
Adnroid 进程保活(三)1像素方案保活
Android 进程保活(四)使用“前台服务”保活
Android 进程保活(五)JobSheduler进程重生