对于进程保活,其实吧,现在对于MIUI、EMUI等等许多高度定制的系统并没有100%的保活方案,该死还是死掉,但是做了一定的操作,还是可以适当的提高存活的。如下就是我用到的保活方案。
1、启动软件的时候激活本地服务和远程服务
1 2 |
startService (new Intent (this, MainService.class)); startService (new Intent (this, RemoteService.class)); |
2、本地服务代码
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 |
/** * content:后台运行的服务 * Actor:韩小呆 * Time:2018/5/3 */ public class MainService extends Service { MyBinder binder; MyConn conn; @Override public IBinder onBind(Intent intent) { return binder; } @Override public void onCreate() { super.onCreate(); binder = new MyBinder(); conn = new MyConn(); } class MyBinder extends IMyAidlInterface.Stub { @Override public String getServiceName() throws RemoteException { return MainService.class.getSimpleName(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { this.bindService(new Intent(MainService.this, RemoteService.class), conn, Context.BIND_IMPORTANT); return START_STICKY; } class MyConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { } @Override public void onServiceDisconnected(ComponentName name) { Intent intent = new Intent(MainService.this, RemoteService.class); if (Build.VERSION.SDK_INT >= 26) { \\适配8.0机制 MainService.this.startForegroundService(intent); } else { MainService.this.startService(intent); } //绑定远程服务 MainService.this.bindService(new Intent(MainService.this, RemoteService.class), conn, Context.BIND_IMPORTANT); } } @Override public void onDestroy() { super.onDestroy(); Intent intent = new Intent(MainService.this, RemoteService.class); \\适配8.0机制 if (Build.VERSION.SDK_INT >= 26) { MainService.this.startForegroundService(intent); } else { MainService.this.startService(intent); } MainService.this.bindService(new Intent(MainService.this, RemoteService.class), conn, Context.BIND_IMPORTANT); } } |
3、远程服务代码
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 76 77 |
/** * content:后台运行的服务 * Actor:韩小呆 * Time:2018/5/3 */ public class RemoteService extends Service { MyConn conn; MyBinder binder; @Override public IBinder onBind(Intent intent) { return binder; } @Override public void onCreate() { super.onCreate(); conn = new MyConn(); binder = new MyBinder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { this.bindService(new Intent(this, MainService.class), conn, Context.BIND_IMPORTANT); return START_STICKY; } class MyBinder extends IMyAidlInterface.Stub { @Override public String getServiceName() throws RemoteException { return RemoteService.class.getSimpleName(); } } class MyConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { } @Override public void onServiceDisconnected(ComponentName name) { Intent intent = new Intent(RemoteService.this, MainService.class); //开启本地服务 if (Build.VERSION.SDK_INT >= 26) { \\适配8.0机制 RemoteService.this.startForegroundService(intent); } else { RemoteService.this.startService(intent); } //绑定本地服务 RemoteService.this.bindService(new Intent(RemoteService.this, MainService.class), conn, Context.BIND_IMPORTANT); } } @Override public void onDestroy() { super.onDestroy(); Intent intent = new Intent(RemoteService.this, MainService.class); //开启本地服务 if (Build.VERSION.SDK_INT >= 26) { \\适配8.0机制 RemoteService.this.startForegroundService(intent); } else { RemoteService.this.startService(intent); } //绑定本地服务 RemoteService.this.bindService(new Intent(RemoteService.this, MainService.class), conn, Context.BIND_IMPORTANT); } } |
4、创建AIDL实现远近程服务通信
1 2 3 4 5 |
// Declare any non-default types here with import statements interface IMyAidlInterface { String getServiceName(); } |
2018.09.22 07:05
@安卓猿 主动清理那肯定是不行的, 用户的手机必须得用户自己作主,如果开发者能随便常驻后台, 那这样的手机 以后是没有人敢买的, 开发者会在这样的手机上疯狂后台消耗资源,国内开发者就是这样的素质
2018.09.22 08:26
@吾乃韩小呆 另外,你这里能作用的也就是这个 粘性服务 有点作用
2018.09.22 08:27
@安卓猿 嗯,是的,我就需要这个粘性,剩下的其他的后台拉活方式,额,文章很多,还有我并不想搞一个流氓软件?
2018.09.22 08:37
楼主,你这样的保活应该没有效果