http://blog.csdn.net/liuyangqiao/article/details/52768813
基本原理,服务A绑定服务B,某一方发现绑定解除,再去开启另一方,其中涉及aidl,待研究
1。本地服务
package com.example.shibo.testshuangjincheng.service;
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.widget.Toast;
import myaidl.MyAidlInterface;
/**
* Created by shibo on 2017/3/28.
*/
public class RemoteService 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, " 远程服务started", Toast.LENGTH_SHORT).show();
this.bindService(new Intent(this, LocalService.class), conn, Context.BIND_IMPORTANT);
return START_STICKY;
}
class MyBinder extends MyAidlInterface.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) {
Toast.makeText(RemoteService.this, "本地服务killed", Toast.LENGTH_SHORT).show();
//开启本地服务
RemoteService.this.startService(new Intent(RemoteService.this, LocalService.class));
//绑定本地服务
RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
}
}
@Override
public void onDestroy() {
super.onDestroy();
//开启本地服务
RemoteService.this.startService(new Intent(RemoteService.this, LocalService.class));
//绑定本地服务
RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
}
}
2。远程服务
package com.example.shibo.testshuangjincheng.service;
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.widget.Toast;
import myaidl.MyAidlInterface;
/**
* Created by shibo on 2017/3/28.
*/
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 MyAidlInterface.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, " 本地服务started", Toast.LENGTH_SHORT).show();
this.bindService(new Intent(LocalService.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) {
Toast.makeText(LocalService.this, "远程服务killed", Toast.LENGTH_SHORT).show();
//开启远程服务
LocalService.this.startService(new Intent(LocalService.this, RemoteService.class));
//绑定远程服务
LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
}
}
@Override
public void onDestroy() {
super.onDestroy();
//开启远程服务
LocalService.this.startService(new Intent(LocalService.this, RemoteService.class));
//绑定远程服务
LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
}
}
3。aidl
// MyAidlInterface.aidl
package myaidl;
// Declare any non-default types here with import statements
interface MyAidlInterface {
String getServiceName();
}
4.manifest
<service android:name=".service.LocalService"
android:enabled="true"
android:exported="true"/>
<service android:name=".service.RemoteService"
android:process=":romoteservice"
android:enabled="true"
android:exported="true"
/>