使用双进程实现Android APP保活
Android APP保活是一个很重要的问题,很多APP需要在后台持续运行,比如在后台持续定位、即时通讯、播放音乐等,但是Android系统为了省电,经常把在后台运行的进程杀死,造成APP不能保活,影响功能和使用.因此需要进行APP保活,APP保活有很多方法,如设置白名单,但是效果不是很好,即使设置了进程也可能被杀死,还有就是设置闹钟,但是会把闹钟搞乱,用户看一眼闹钟设置就能看出来,相对而言,使用双进程保活是一种比较好的方法,不容易被杀死,对用户也没有影响.
双进程保活首先要定义两个Service,并且让这两个Service在不同的进程中运行.这里定义了LocalService和RemoteService,其中LocalService在主进程中运行,RemoteService另外新建一个进程.
LocalService:
public class Local_Service extends Service { private Handler handler; private Runnable runnable; @Override public void onCreate() { super.onCreate(); // 初始化工作 handler = new Handler(); runnable = new Runnable() { @Override public void run() { try { // 执行任务操作 } catch (ClassNotFoundException e) { throw new RuntimeException(e); } handler.postDelayed(this, 3000); } }; } @Override public IBinder onBind(Intent intent) { return new LocalBinder(); } @Override public int onStartCommand(Intent intent, int flags, int start_id) { handler.postDelayed(runnable, 3000); bindService(new Intent(this, RemoteService.class), connection, Context.BIND_AUTO_CREATE); return super.onStartCommand(intent, flags, start_id); } private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // 绑定成功 } @Override public void onServiceDisconnected(ComponentName name) { // 当RemoteService所处进程被干掉就重新启动 startService(new Intent(LocalService.this, RemoteService.class)); bindService(new Intent(LocalService.this, RemoteService.class), connection, Context.BIND_IMPORTANT); } }; private class LocalBinder extends Binder { } }
RemoteService:
public class Remote_Service extends Service { private Handler handler; private Runnable runnable; private boolean is_map_service_running; private boolean is_pos_service_running; public Remote_Service() { } @Override public void onCreate() { super.onCreate(); // 初始化工作 handler = new Handler(); runnable = new Runnable() { @Override public void run() { try { // } catch (ClassNotFoundException e) { throw new RuntimeException(e); } // 执行任务操作 handler.postDelayed(this, 3000); } }; } @Override public IBinder onBind(Intent intent) { return new RemoteBinder(); } @Override public int onStartCommand(Intent intent, int flags, int start_id) { handler.postDelayed(runnable, 3000); bindService(new Intent(this, LocalService.class), connection, Context.BIND_AUTO_CREATE); return super.onStartCommand(intent, flags, start_id); } private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // 绑定成功 } @Override public void onServiceDisconnected(ComponentName name) { // 当LocalService所处进程被干掉就重新启动 startService(new Intent(RemoteService.this, LocalService.class)); bindService(new Intent(RemoteService.this, LocalService.class), connection, Context.BIND_IMPORTANT); } }; private class RemoteBinder extends Binder { } }
两个Service相互绑定,如果其中一个Service所在的进程被kill了,另外一个就对回调onServiceDisconnected()方法,重新启动被kill的进程,这样就保证了这两个Service能够一直运行.
在AndroidManifest.xml中定义两个Service:
<service android:name=".Service.LocalService" /> <service android:name=".Service.RemoteService" android:enabled="true" android:exported="true" android:process=":remote" />
其中LocalService在主进程中运行,RemoteService通过android:process=":remote"指定了在另外一个进程中运行.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2024-01-30 Failed to initialize NVML: Driver/library version mismatch