Android双进程守护service保活
来源:互联网 发布:云计算培训沈阳 编辑:程序博客网 时间:2024/05/16 07:14
android应用进程保活可以从以下来年个方面考虑
1、尽量保证应用进程不被杀死。
2、进程被杀死后能够复活。
要保证进程不被杀死是不可能的,系统在资源紧缺的情况下回根据进程优先等级杀掉有限等级比较低的进程,所以为了使自己的进程不易于被系统杀死,只能提高进程的优先级,这个进程重要性的层次结构有五个等级,按高到低分为
1)前台进程 Foreground process
2)可见进程 Visible process
3)服务进程 Service process
4)后台进程 Background process
5)空进程 Empty process
Android的service置为前台startForeground,这样就提高了service的优先级。
系统总会在某个时刻因为某些原因把你杀掉,被杀掉不可怕,可怕的是被杀掉后就再也活不过来了,so我们得制定各种策略,好让进程能在被杀后可以自启。进程被杀死后重启复活我们通过双进程守护可以是可以做到的,通过双进程守护可以在大部分情况下做到进程保活,手动清理和360等第三方软件的清理仍可以保活,对于要求比较高的场合,双进程守护是不够的,还要进行更深入的研究,系统清理进程不在本文的考虑范围之内,有兴趣的朋友可以自己去深入研究。
双进程守护的实现:
在应用中开启两个Service分别为LocalService和RemoteService,通过aidl相互绑定,只要其中一个呗杀死则通过另一个重启对方,这就保证了Service保活,关键的一点事LocalService和RemoteService必须要在两个不同的而进程中,我们可以通过在AndroidManifest.xml配置文件中定义
<service android:name=".LocalService" />
<service android:name=".RemoteService" android:process=":remote"/>
android:process=":remote"可以保证RemoteService运行在另外一个进程中。
下面贴出双进程守护保活的demo代码,供参考
在包路径下定义aidl文件夹aidl
在文件夹下定义Guard.aidl文件
package com.guardservice.aidl;interface GuardAidl{ void doSomething();}
本地服务LocalService.java
package com.guardservice;import com.guardservice.aidl.GuardAidl;import android.app.Notification;import android.app.PendingIntent;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 LocalService extends Service {private MyBilder mBilder;@Overridepublic IBinder onBind(Intent intent) {if (mBilder == null)mBilder = new MyBilder();return mBilder;}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {this.bindService(new Intent(LocalService.this, RemoteService.class),connection, Context.BIND_ABOVE_CLIENT);Notification notification = new Notification(R.drawable.ic_launcher,"安全服务启动中", System.currentTimeMillis());PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,0);notification.setLatestEventInfo(this, "安全服务", "安全服务...", pendingIntent);startForeground(startId, notification);return START_STICKY;}private class MyBilder extends GuardAidl.Stub {@Overridepublic void doSomething() throws RemoteException {Log.i("TAG", "绑定成功!");Intent localService = new Intent(LocalService.this,RemoteService.class);LocalService.this.startService(localService);LocalService.this.bindService(new Intent(LocalService.this,RemoteService.class), connection,Context.BIND_ABOVE_CLIENT);}}private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i("TAG", "RemoteService被杀死了");Intent localService = new Intent(LocalService.this,RemoteService.class);LocalService.this.startService(localService);LocalService.this.bindService(new Intent(LocalService.this,RemoteService.class), connection,Context.BIND_ABOVE_CLIENT);}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.i("TAG", "RemoteService链接成功!");try {if (mBilder != null)mBilder.doSomething();} catch (RemoteException e) {e.printStackTrace();}}};}
远程服务RemoteService.java
package com.guardservice;import com.guardservice.aidl.GuardAidl;import android.app.Notification;import android.app.PendingIntent;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 MyBilder mBilder;//private final static int GRAY_SERVICE_ID = 1002;@Overridepublic IBinder onBind(Intent intent) {if (mBilder == null)mBilder = new MyBilder();return mBilder;}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// 在RemoteService运行后,我们对LocalService进行绑定。 把优先级提升为前台优先级this.bindService(new Intent(RemoteService.this, LocalService.class),connection, Context.BIND_ABOVE_CLIENT);Notification notification = new Notification(R.drawable.ic_launcher,"安全服务启动中", System.currentTimeMillis());PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,0);notification.setLatestEventInfo(this, "安全服务", "安全服务...", pendingIntent);startForeground(startId, notification);return START_STICKY;}private class MyBilder extends GuardAidl.Stub {@Overridepublic void doSomething() throws RemoteException {Log.i("TAG", "绑定成功!");}}private ServiceConnection connection = new ServiceConnection() {/** * 在终止后调用,我们在杀死服务的时候就会引起意外终止,就会调用onServiceDisconnected * 则我们就得里面启动被杀死的服务,然后进行绑定 */@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i("TAG", "LocalService被杀死了");Intent remoteService = new Intent(RemoteService.this,LocalService.class);RemoteService.this.startService(remoteService);RemoteService.this.bindService(new Intent(RemoteService.this,LocalService.class), connection, Context.BIND_ABOVE_CLIENT);}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.i("TAG", "LocalService链接成功!");try {if (mBilder != null)mBilder.doSomething();} catch (RemoteException e) {e.printStackTrace();}}};}
- Android双进程守护service保活
- 进程保活之双进程守护
- Android 进程保活,Service进程常驻
- (4.6.17.2)进程保活(一补录、双服务和双进程):保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护
- 进程保活-双进程守护的正确姿势
- android守护进程、常驻服务保活技术
- 进程保活之单进程守护
- 安卓应用保活实践(双进程守护)
- 新型的双进程守护应用保活方法
- Android 永生不死的进程,进程守护,进程常驻,进程保活
- android保活进程总结--双进程保活策略
- service双进程守护
- Service的进程保活
- Android 进程常驻、进程守护、进程保活技术的总结
- Android Service后台进程守护
- Android组件保活,service保活
- Android进程保活
- Android 进程保活
- mysql 存储过程使用示例
- Service Worker 入门 - PWA 强依赖于 Service Worker
- vs2015常用快捷键
- 沉思录
- i++和++i是怎么用的?它们作为函数参数时的使用举例
- Android双进程守护service保活
- Service Worker API (mozilla) vs Service Workers (google)
- 浅析回调
- typedef 和#define 的区别
- 03 建立单独的KVM虚拟化
- 欢迎使用CSDN-markdown编辑器
- 思维晚操-12.19
- writeup--RedTiger's Hackit
- MyBatis学习之环境搭建