Android进程保活主流方案(1)

本文介绍了在Android中通过前台Service、使用BroadcastReceiver进行单进程广播守护、AIDL实现双进程守护以及利用NDK通过C++进程守护来保持应用在后台运行的方法。作者还分享了项目结构和实际操作示例,以及注意事项如版本兼容性等。
摘要由CSDN通过智能技术生成

@Override
protected void onStop() {
Log.d(MainActivity.TAG, “MainActivity onStop”);
super.onStop();
}

@Override
protected void onDestroy() {
KeepAliveManager.getInstance().unregisterKeepLiveReceiver(this);
super.onDestroy();
}
}

运行,发现在笔者的一加6T(Android 10)下应用内熄屏可以拉起1像素保活Activity,但按Home回到主页后,再熄屏虽然可以接到广播,但无法拉起Activity:

设置前台Service

本方案原理是拥有前台Service的进程将拥有更高的优先级,也就更难被回收掉。注意:部分Android版本前台Service会在顶部通知栏露出“马脚”:

可以试着在前台Service创建通知后立马清除它来隐藏自己。利用相同id前台Service会使用同一条通知的特性,创建一个与前台Service有相id的“替罪羊”前台Service,然后结束它,这样通知会被随之清除,但原本的Service可以继续工作。

项目结构如下。

AndroidManifest.xml

//不加该权限会报错:java.lang.SecurityException: Permission Denial: startForeground from pid=XXX, uid=XXX requires android.permission.FOREGROUND_SERVICE


ForegroundService :模拟工作的(前台)线程,如有必要会开启ScapegoatService来清除通知栏通知。由于缺乏更多高版本系统样本,Android 8.0以上部分可能不靠谱,不过推荐还是老老实实按谷歌要求发出通知为妙。

package com.zyc.foregroundservice;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.util.Timer;
import java.util.TimerTask;

public class ForegroundService extends Service {
private static final int SERVICE_ID = 1;
private Timer timer;
private TimerTask timerTask;
public static int count;

public ForegroundService() {
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
Log.d(MainActivity.TAG, “创建前台服务”);
}

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startTask();

//判断版本
if (Build.VERSION.SDK_INT < 18) {//Android4.3以下版本
//直接调用startForeground即可,不会在通知栏创建通知
startForeground(SERVICE_ID, new Notification());

} else if (Build.VERSION.SDK_INT < 24) {//Android4.3 - 7.0之间
Intent scapegoatIntent = new Intent(this, ScapegoatService.class);
startService(scapegoatIntent);

} else {//Android 8.0以上
//经测试,本人的一加6T(Android 10)这样写并不会在通知栏创建通知,其他机型与版本效果仍需考证
startForeground(SERVICE_ID, new Notification());
}
return START_STICKY;
}

/**

  • 开启定时任务,count每秒+1
    */
    private void startTask() {
    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;
    }

@Override
public void onDestroy() {
stopTask();
Log.d(MainActivity.TAG, “销毁前台服务”);
super.onDestroy();
}
}

ScapegoatService :拥有和工作线程相同id,启动后立马自行停止并销毁,也就替ForegroundService 消除了通知。

package com.zyc.foregroundservice;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ScapegoatService extends Service {
private static final int SERVICE_ID = 1; //后台ForegroundService的SERVICE_ID相同

public ScapegoatService() {
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
Log.d(MainActivity.TAG, “创建前台服务替身”);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(SERVICE_ID, new Notification());
stopForeground(true);//移除通知栏消息
stopSelf();
return START_STICKY;
}

@Override
public void onDestroy() {
Log.d(MainActivity.TAG, “销毁前台服务替身”);
super.onDestroy();
}
}

MainActivity :启动/停止Service而已,不多赘述。

package com.zyc.foregroundservice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
public static String TAG = “MyLog”;
private Intent intent;

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

intent = new Intent(this, ForegroundService.class);
startService(intent);
}

@Override
protected void onDestroy() {
stopService(intent);
super.onDestroy();
}
}

运行,在Android 5.1.1下可以以此消除通知栏Service提示。
[图片上传失败…(image-3f45a3-1603077932504)]

单进程广播守护

本方案主要是通过Service销毁发出广播通知BroadcastReceiver“复活”自己实现进程保活。

项目结构如下。

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:

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

移动架构师

系统学习技术大纲

一线互联网Android面试题总结含详解(初级到高级专题)

image

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

DK实现。本方案主要是使用C++ fork()一个子进程来保护APP进程,两者通过socket连接,一旦APP进程被杀死,socket连接断开,守护进程就知道该启动APP进程了。PS:采用socket方案要比轮询更节省资源。
[外链图片转存中…(img-be4sEBKY-1714459837989)]

项目结构如下。
[图片上传失败…(image-f5fb9c-1603078827743)]

AndroidManifest.xml:

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

[外链图片转存中…(img-JJllTtSe-1714459837989)]

[外链图片转存中…(img-8tmdSdUT-1714459837989)]

一线互联网Android面试题总结含详解(初级到高级专题)

[外链图片转存中…(img-KLT1vW4v-1714459837990)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 30
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 进程保活是指应用程序保持在后台运行,即使用户关闭了应用程序的可见界面,仍然能够接收到系统消息或执行定时任务。这通常是通过以下几个机制实现的: 1. **Service**:Service 是 Android 中的一种组件,可以在后台长期运行并执行一些任务,如接收广播、位置更新等。服务可以通过startService()启动,并使用stopSelf()或bindService()来控制其生命周期。 2. **BroadcastReceiver**:应用程序可以通过创建 BroadcastReceiver 来监听特定的系统或自定义事件,当这些事件发生时,即使应用被关闭,广播也会将消息传递给服务或活动,从而间接实现进程保活。 3. **JobScheduler**:Android 6.0(API level 23)以后引入了 JobScheduler API,开发者可以安排定期的任务在后台执行,即使设备处于空闲状态也可以执行。 4. **AlarmManager**:虽然 AlarmManager 不直接支持保活,但结合Service或WakefulBroadcastReceiver可以设置周期性的闹钟,使得应用在指定时间被唤醒,间接实现进程保活。 5. **Foreground Services**:从 Android Oreo (API level 26)开始,Google引入了 Foreground Services,这些服务需要显示通知,即使在后台也能保持运行。 6. **后台任务栈**:通过管理ActivityStack,应用程序可以创建一个堆栈,当用户关闭应用时,任务栈中的顶部活动会被保存,直到用户再次启动应用。 要避免过度保活导致的资源消耗和用户体验下降,还需要注意遵守Android的后台操作策略和权限管理规范。同时,根据用户行为和需求选择合适的保活策略是非常重要的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值