昨天看了个公开课,使用双进程和aidl来实现进程保活,今天来实现以下
首先展示下结果:
建立两个不同进程的service
一个在应用进程,另一个新开了一个进程。
<service android:name=".LocalService"></service>
<service android:name=".RomoteService"
android:process=":romoteservice"></service>
建立aidl实现进程间通讯,使用studio写完aidl中记得同步以下工程来生成gen下面的代码
在服务类中调用aidl存根
class MyBinder extends IMyAidlInterface.Stub {
@Override
public String getServiceName() throws RemoteException {
return LocalService.class.getSimpleName();
}
}
在开启服务的时候绑定另一个服务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(LocalService.this, " 本地服务活了", Toast.LENGTH_SHORT).show();
this.bindService(new Intent(LocalService.this,RomoteService.class),conn, Context.BIND_IMPORTANT);
return START_STICKY;
}
和另一个服务建立上连接之后就可根据连接是否中断来判断两一个服务是否还活着,如果断开则已经挂掉了,此时去开启并在次绑定服务
class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("yangqing", "绑定上了远程服务");
}
//连接中断调用次方法
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("yangqing", "远程服务被干掉了");
Toast.makeText(LocalService.this, "远程服务挂了", Toast.LENGTH_SHORT).show();
//开启远程服务
LocalService.this.startService(new Intent(LocalService.this,RomoteService.class));
//绑定远程服务
LocalService.this.bindService(new Intent(LocalService.this,RomoteService.class),conn,Context.BIND_IMPORTANT);
}
}
两个服务代码时类似的。
详细代码:
本地服务进程代码如下:
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.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* Created by dell on 2016/9/20.
*/
public class LocalService extends Service {
MyBinder binder;
MyConn conn;
@Nullable
@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 LocalService.class.getSimpleName();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(LocalService.this, " 本地服务活了", Toast.LENGTH_SHORT).show();
this.bindService(new Intent(LocalService.this,RomoteService.class),conn, Context.BIND_IMPORTANT);
return START_STICKY;
}
class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("yangqing", "绑定上了远程服务");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("yangqing", "远程服务被干掉了");
Toast.makeText(LocalService.this, "远程服务挂了", Toast.LENGTH_SHORT).show();
//开启远程服务
LocalService.this.startService(new Intent(LocalService.this,RomoteService.class));
//绑定远程服务
LocalService.this.bindService(new Intent(LocalService.this,RomoteService.class),conn,Context.BIND_IMPORTANT);
}
}
@Override
public void onDestroy() {
super.onDestroy();
//开启远程服务
LocalService.this.startService(new Intent(LocalService.this,RomoteService.class));
//绑定远程服务
LocalService.this.bindService(new Intent(LocalService.this,RomoteService.class),conn,Context.BIND_IMPORTANT);
}
}
远程服务进程代码
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.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* Created by dell on 2016/9/20.
*/
public class RomoteService extends Service {
MyConn conn;
MyBinder binder;
@Nullable
@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) {
Toast.makeText(this, " 远程服务活了", Toast.LENGTH_SHORT).show();
this.bindService(new Intent(this, LocalService.class), conn, Context.BIND_IMPORTANT);
return START_STICKY;
}
class MyBinder extends IMyAidlInterface.Stub {
@Override
public String getServiceName() throws RemoteException {
return RomoteService.class.getSimpleName();
}
}
class MyConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("yangqing", "绑定本地服务成功");
// Toast.makeText(RomoteService.this, "绑定本地服务成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("yangqing", "本地服务被干掉了");
Toast.makeText(RomoteService.this, "本地服务挂了", Toast.LENGTH_SHORT).show();
//开启本地服务
RomoteService.this.startService(new Intent(RomoteService.this, LocalService.class));
//绑定本地服务
RomoteService.this.bindService(new Intent(RomoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
}
}
@Override
public void onDestroy() {
super.onDestroy();
//开启本地服务
RomoteService.this.startService(new Intent(RomoteService.this, LocalService.class));
//绑定本地服务
RomoteService.this.bindService(new Intent(RomoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
}
}