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;
@Override
public IBinder onBind(Intent intent) {
if (mBilder == null)
mBilder = new MyBilder();
return mBilder;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public 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 {
@Override
public 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() {
@Override
public 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);
}
@Override
public 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;
@Override
public IBinder onBind(Intent intent) {
if (mBilder == null)
mBilder = new MyBilder();
return mBilder;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public 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 {
@Override
public void doSomething() throws RemoteException {
Log.i("TAG", "绑定成功!");
}
}
private ServiceConnection connection = new ServiceConnection() {
/**
* 在终止后调用,我们在杀死服务的时候就会引起意外终止,就会调用onServiceDisconnected
* 则我们就得里面启动被杀死的服务,然后进行绑定
*/
@Override
public 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);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("TAG", "LocalService链接成功!");
try {
if (mBilder != null)
mBilder.doSomething();
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
}
156




被折叠的 条评论
为什么被折叠?



