AndroidManifest.xml:
KeepAliveReceiver :为啥用BroadcastReceiver 而不直接在 Service的onDestroy()方法中重启服务?因为这种方式启动的Service仍旧和原进程"在一起",会被一并杀死,而BroadcastReceiver 接到广播启动的则与原进程有隔离性,可以存活。
package com.zyc.demo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class KeepAliveReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(MainActivity.LOG_TAG, “收到保活广播”);
if (!MainActivity.isServiceRunning(context.getApplicationContext(), KeepAliveService.class)) {
Log.d(MainActivity.LOG_TAG, “检测到服务未在运行,启动服务”);
Intent serviceIntent = new Intent(context, KeepAliveService.class);
context.startService(serviceIntent);
} else {
Log.d(MainActivity.LOG_TAG, “检测到服务正在运行,无需再次启动”);
}
}
}
KeepAliveService :该Service会在创建后会对变量count 执行 +1/秒 定时任务,在服务终止/创建时会保存/读取count ,以保证count 的状态不会丢失。保活手段体现在:
- onStartCommand()返回 START_STICKY,Service所在进程被杀死后系统会尝试再次启动,但具体启动时机由系统决定。
- onDestroy()方法发送出Broadcast,等KeepAliveReceiver 收到后来启动自己。
package com.zyc.demo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class KeepAliveService extends Service {
private Timer timer;
private TimerTask timerTask;
public static int count;
@Override
public void onCreate() {
Log.d(MainActivity.LOG_TAG, “服务创建了”);
int save = SharedPreferenceTool.getInstance(getApplicationContext()).getInt(“count”, -1);
if (save == -1) {
this.count = 0;
Log.d(MainActivity.LOG_TAG, “count首次启动,从0开始计数”);
} else {
this.count = save;
Log.d(MainActivity.LOG_TAG, “count从上次保存的 " + save + " 开始计数”);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startTask();
Log.d(MainActivity.LOG_TAG, “服务启动了”);
return START_STICKY;
}
/**
- 开启定时任务,count每秒+1
*/
private void startTask() {
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
Log.d(MainActivity.LOG_TAG, "服务运行中,count: " + count);
count++;
}
};
timer.schedule(timerTask, 0, 1000);
}
/**
- 结束定时任务
*/
private void stopTask() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
count = 0;
}
@Override
public void onDestroy() {
stopTask();
Log.d(MainActivity.LOG_TAG, “服务停止了”);
Intent intent = new Intent(this, KeepAliveReceiver.class);
sendBroadcast(intent);
Log.d(MainActivity.LOG_TAG, “发送保活广播”);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
MainActivity :主要用于开启Service,并提供了判断指定Service是否运行的工具方法。count放在MainActivity 而不是Service是因为Service的onDestroy()在异常结束时不一定被调用。
package com.zyc.demo;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public static String LOG_TAG = “MyLog”;
private Intent serviceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!isServiceRunning(getApplicationContext(), KeepAliveService.class)) {
Log.d(LOG_TAG, “检测到服务未在运行,启动服务”);
serviceIntent = new Intent(this, KeepAliveService.class);
startService(serviceIntent);
} else {
Log.d(LOG_TAG, “检测到服务正在运行,无需再次启动”);
}
}
/**
- 判断某个Service是否在运行
- @param context
- @param serviceClass 需要查看的Service的Class
- @return
*/
public static boolean isServiceRunning(Context context, Class serviceClass) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : activityManager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(runningServiceInfo.service.getClassName())) {
return true;
}
}
return false;
}
@Override
protected void onDestroy() {
if (serviceIntent != null) {
stopService(serviceIntent);
}
SharedPreferenceTool.getInstance(getApplicationContext()).putInt(“count”, KeepAliveService.count);
Log.d(LOG_TAG, “count保存了”);
super.onDestroy();
}
}
SharedPreferenceTool :一个用于保存例中count的SharedPreference工具类。切记用context.getApplicationContext()传入Context ,因为这里的instance是一个static且强引用的,如果随处使用XXXActivity.this传入Context ,这些Activity会随着instance的全局存在而难以回收,最终将造成内存泄漏。
package com.zyc.demo;
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPreferenceTool {
public static SharedPreferenceTool instance;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private SharedPreferenceTool(Context context) {
sharedPreferences = context.getSharedPreferences(“preferences”, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
public static SharedPreferenceTool getInstance(Context context) {
if (instance == null) {
synchronized (SharedPreferenceTool.class) {
if (instance == null) {
// 使用双重同步锁
instance = new SharedPreferenceTool(context);
}
}
}
return instance;
}
/**
- 往SharedPreference存放整型数据
*/
public void putInt(String key, int value) {
editor.putInt(key, value);
editor.commit();
}
/**
- 从SharedPreference取出整型数据
*/
public int getInt(String key, int defaultValue) {
return sharedPreferences.getInt(key, defaultValue);
}
}
运行,虽然有时Service重启并不及时,但在Android 5.1.1总体而言count被很大程度保留在后台运转。但此方法无法对抗 设置–应用程序–强制停止,也无法在Android 10下拉起服务。
[图片上传失败…(image-cf605e-1603077932501)]
AIDL双进程守护
本方案主要是两个(不同进程)Service通过AIDL“互相拉起”用于实现进程保活。
项目结构如下。
AndroidManifest.xml:
AIDL:
package com.zyc.aidlkeepalive;
interface IKeepAliveAidlInterface {}
LocalService:与进程RemoteService相互绑定,当RemoteService被终止导致onServiceDisconnected()方法被触发时再次将其启动。
package com.zyc.aidlkeepalive;
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.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class LocalService extends Service {
private IBinder binder;
private ServiceConnection serviceConnection;
private Timer timer;
private TimerTask timerTask;
private int count = 0;
public LocalService() {}
@Override
public void onCreate() {
super.onCreate();
Log.d(MainActivity.TAG, “LocalService onCreate”);
binder = new LocalServiceBinder();
serviceConnection = new LocalServiceConnection();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(MainActivity.TAG, “LocalService onStartCommand”);
startTask();
wakeService();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
Log.d(MainActivity.TAG, “LocalService onBind”);
return binder;
}
@Override
public void onDestroy() {
stopTask();
stopSelf();
unbindService(serviceConnection);
Log.d(MainActivity.TAG, “LocalService onDestroy”);
super.onDestroy();
}
private void wakeService() {
if (!MainActivity.isServiceRunning(this, RemoteService.class)) {
startService(new Intent(this, RemoteService.class));
}
bindService(new Intent(this, RemoteService.class), serviceConnection, Context.BIND_IMPORTANT);
}
class LocalServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(MainActivity.TAG, “触发LocalService onServiceConnected”);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(MainActivity.TAG, “触发LocalService onServiceDisconnected”);
wakeService();
}
}
class LocalServiceBinder extends IKeepAliveAidlInterface.Stub {}
/**
- 开启定时任务,count每秒+1
*/
private void startTask() {
count = 0;
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
Log.d(MainActivity.TAG, "服务运行中,count: " + count);
count++;
}
};
timer.schedule(timerTask, 0, 1000);
}
/**
- 结束定时任务
*/
private void stopTask() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
count = 0;
}
}
RemoteService:与LocalService作用基本相同,不多赘述。
package com.zyc.aidlkeepalive;
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.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class RemoteService extends Service {
private IBinder binder;
private ServiceConnection serviceConnection;
private Timer timer;
private TimerTask timerTask;
private int count = 0;
public RemoteService() {}
@Override
public void onCreate() {
super.onCreate();
Log.d(MainActivity.TAG, “RemoteService onCreate”);
binder = new RemoteServiceBinder();
serviceConnection = new RemoteServiceConnection();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(MainActivity.TAG, “RemoteService onStartCommand”);
startTask();
wakeService();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
Log.d(MainActivity.TAG, “RemoteService onBind”);
return binder;
}
@Override
public void onDestroy() {
stopTask();
stopSelf();
unbindService(serviceConnection);
Log.d(MainActivity.TAG, “RemoteService onDestroy”);
super.onDestroy();
}
class RemoteServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(MainActivity.TAG, “触发RemoteService onServiceConnected”);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(MainActivity.TAG, “触发RemoteService onServiceDisconnected”);
wakeService();
}
}
private void wakeService() {
if (!MainActivity.isServiceRunning(this, LocalService.class)) {
startService(new Intent(this, LocalService.class));
}
bindService(new Intent(this, LocalService.class), serviceConnection, Context.BIND_IMPORTANT);
}
class RemoteServiceBinder extends IKeepAliveAidlInterface.Stub {
}
/**
- 开启定时任务,count每秒+1
*/
private void startTask() {
count = 0;
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
Log.d(MainActivity.TAG, "服务运行中,count: " + count);
count++;
}
};
timer.schedule(timerTask, 0, 1000);
}
/**
- 结束定时任务
*/
private void stopTask() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
count = 0;
}
}
MainActivity:
package com.zyc.aidlkeepalive;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
public static final String TAG = “MyLog”;
private Intent intentLocal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentLocal = new Intent(this, LocalService.class);
startService(intentLocal);
}
public static boolean isServiceRunning(Context context, Class serviceClass) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : activityManager.getRunningServices(Integer.MAX_VALUE)) {
if (runningServiceInfo.service.getClassName().equals(serviceClass.getName())) {
return true;
}
}
return false;
}
}
运行,在Android 5.1.1两个进程可以在一定程度上相互拉起(不是100%),但无法抵抗应用–强制终止。
[图片上传失败…(image-2d95ad-1603077932499)]
NDK双进程守护
由于NDK进程优先级非常高,所以我们可以把上面的Java双进程守护放到NDK实现。本方案主要是使用C++ fork()一个子进程来保护APP进程,两者通过socket连接,一旦APP进程被杀死,socket连接断开,守护进程就知道该启动APP进程了。PS:采用socket方案要比轮询更节省资源。
项目结构如下。
[图片上传失败…(image-f5fb9c-1603078827743)]
AndroidManifest.xml:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.1)
add_library(
native-lib
SHARED
native-lib.cpp)
find_library(
log-lib
log)
target_link_libraries(
native-lib
${log-lib})
Guard:声明JNI方法。
package com.zyc.doubleguard;
public class Guard {
static {
System.loadLibrary(“native-lib”);
}
public native void create(String userId);
public native void connect();
}
WorkService:模拟APP要被守护的工作服务,不停打印Log。
package com.zyc.doubleguard;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class WorkService extends Service {
private static int count = 0;
private Guard guard;
private Timer timer;
private TimerTask timerTask;
public WorkService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(“MyLog”, “服务创建”);
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
Log.i(“MyLog”, “服务进行中 count=” + count);
count++;
}
};
guard = new Guard();
guard.create(String.valueOf(Process.myUid()));
guard.connect();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(“MyLog”, “服务启动”);
startWork();
return START_STICKY;
}
@Override
public void onDestroy() {
stopWork();
Log.d(“MyLog”, “服务被销毁”);
super.onDestroy();
}
private void startWork() {
timer.schedule(timerTask, 0, 3000);
}
private void stopWork() {
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
if (timer != null) {
timer.cancel();
timer = null;
}
}
}
MainActivity:只用来启动服务,不多赘述。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, WorkService.class);
startService(intent);
}
native-lib.cpp:提供了开启子进程、子进程开启socket服务端、客户端连接socket方法。
#include <jni.h>
#include
#include
#include <android/log.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <arpa/inet.h>
#define LOGE(…) __android_log_print(ANDROID_LOG_ERROR, “MyLog”, VA_ARGS)
void guard_start_work();
bool guard_create();
void guard_read_msg();
const char *userId;
/**
- 用于创建一个守护进程
*/
extern “C”
JNIEXPORT void JNICALL
Java_com_zyc_doubleguard_Guard_create(JNIEnv *env, jobject thiz, jstring user_id) {
userId = env->GetStringUTFChars(user_id, 0);
//开进程
pid_t pid = fork();
if (pid < 0) {
LOGE(“Guard开进程失败”);
} else if (pid == 0) {//子进程
guard_start_work();
} else if (pid > 0) {//父进程
}
env->ReleaseStringUTFChars(user_id, userId);
}
/**
- 守护进程 - 开始
*/
void guard_start_work() {
if (guard_create()) {
guard_read_msg();
}
}
const char *PATH = “/data/data/com.zyc.doubleguard/guard.socket”;
int server_lfd = -1;
int server_cfd = -1;
/**
- 守护进程 - 创建socket
*/
bool guard_create() {
server_lfd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (server_lfd < 0) {
LOGE(“Guard socket 初始化错误”);
return false;
}
unlink(PATH);//把之前连接的服务端清空
struct sockaddr_un addr;
bzero(&addr, sizeof(sockaddr_un));
addr.sun_family = AF_LOCAL;
strcpy(addr.sun_path, PATH);
int bind_res = bind(server_lfd, (const sockaddr *) &addr, sizeof(sockaddr_un));
if (bind_res < 0) {
LOGE(“Guard bind错误”);
return false;
}
listen(server_lfd, 5);//可以守护5个app
LOGE(“Guard 开始listen”);
while (true) {
server_cfd = accept(server_lfd, NULL, NULL);
if (server_cfd < 0) {
if (errno == EINTR) {
//client连接失败,重连
continue;
} else {
LOGE(“Guard 读取错误”);
return 0;
}
} else {
LOGE(“进程 %d 连接上了 Guard”, server_cfd);
break;
}
}
return true;
}
最后
以前一直是自己在网上东平西凑的找,找到的东西也是零零散散,很多时候都是看着看着就没了,时间浪费了,问题却还没得到解决,很让人抓狂。
后面我就自己整理了一套资料,还别说,真香!
资料有条理,有系统,还很全面,我不方便直接放出来,大家可以先看看有没有用得到的地方吧。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
EINTR) {
//client连接失败,重连
continue;
} else {
LOGE(“Guard 读取错误”);
return 0;
}
} else {
LOGE(“进程 %d 连接上了 Guard”, server_cfd);
break;
}
}
return true;
}
最后
以前一直是自己在网上东平西凑的找,找到的东西也是零零散散,很多时候都是看着看着就没了,时间浪费了,问题却还没得到解决,很让人抓狂。
后面我就自己整理了一套资料,还别说,真香!
资料有条理,有系统,还很全面,我不方便直接放出来,大家可以先看看有没有用得到的地方吧。
[外链图片转存中…(img-fY2hBqjz-1714459875233)]
[外链图片转存中…(img-am9dpE1y-1714459875234)]
[外链图片转存中…(img-XFKymF1I-1714459875234)]
[外链图片转存中…(img-4lUSF28f-1714459875234)]
[外链图片转存中…(img-kFQJxIni-1714459875235)]
[外链图片转存中…(img-ASmZTuNH-1714459875235)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!