1、ProcessService.aidl
package com.freeman.inter;
interface ProcessService{
String getServiceName();
}
2、本地服务LocalService
public class LocalService extends Service {
public static final String TAG = "Info";
private Context mContext;
private MyBinder binder;
private MyConn conn;
@Override
public void onCreate() {
super.onCreate();
mContext = LocalService.this;
binder = new MyBinder();
if (conn == null) {
conn = new MyConn();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent intentRemote = new Intent(mContext, RemoteService.class);
bindService(intentRemote, conn, Context.BIND_IMPORTANT);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
class MyBinder extends ProcessService.Stub {
@Override
public String getServiceName() throws RemoteException {
return "LocalService";
}
}
class MyConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.i(TAG, "本地连接远程服务!!!");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(mContext, "远程服务被干死了", Toast.LENGTH_LONG).show();
// 启动远程服务、绑定远程服务
Intent intentRemote = new Intent(mContext, RemoteService.class);
mContext.startService(intentRemote);
mContext.bindService(intentRemote, conn, Context.BIND_IMPORTANT);
}
}
}
AndroidManifest.xml文件中加入配置,默认为UI线程
<service android:name=".LocalService" />
3、远程服务RemoteService
public class RemoteService extends Service {
public static final String TAG = "Info";
private Context mContext;
private MyBinder binder;
private MyConn conn;
@Override
public void onCreate() {
super.onCreate();
mContext = RemoteService.this;
binder = new MyBinder();
if (conn == null) {
conn = new MyConn();
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
bindService(new Intent(RemoteService.this, LocalService.class), conn,
Context.BIND_IMPORTANT);
return super.onStartCommand(intent, flags, startId);
}
class MyBinder extends ProcessService.Stub {
@Override
public String getServiceName() throws RemoteException {
return "RemoteService";
}
}
class MyConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
Log.i(TAG, "本地连接远程服务!!!");
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
Toast.makeText(mContext, "本地服务被干死了", Toast.LENGTH_LONG).show();
// 启动本地服务、绑定本地服务
Intent intentLocal = new Intent(mContext, LocalService.class);
mContext.startService(intentLocal);
mContext.bindService(intentLocal, conn, Context.BIND_IMPORTANT);
}
}
}
AndroidManifest.xml文件中加入配置,让该服务成为一个新的线程
<service
android:name=".RemoteService"
android:process=".remoteservice" />
4、在MainActivity启动两个线程
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this,LocalService.class));
startService(new Intent(this,RemoteService.class));
}
解释说明:
1、LocalService
- 1.1 在onCreate()时创建MyBinder对象,MyBinder类是一个远程接口的实现类;然后再创建MyConn对象,MyConn类是LocalService绑定RemoteService后的回调类。
- 1.2 在onStartCommand()方法中以绑定的方式开启RemoteService,第二个参数是MyConn类型的conn,第三个参数设置为“Context.BIND_IMPORTANT”
- 1.3 在onBind中返回binder对象
- 1.4 由于MyConn类是绑定后回调类,在绑定成功后会执行onServiceConnected()方法,在该方法中可以处理相应的业务逻辑。重点是在onServiceDisconnected()方法上,如果绑定失败或者RemoteService被系统或第三方App杀死后,该方法会执行。这时可以先以startService的方式启动RemoteService,然后再以bindService的方式开启RemoteService
2、RemoteService的实现思路和LocalService是一样的。就是利用两个服务在ServiceConnection的回调实现类MyConn中,在onServiceDisconnected方法中互相以startService和bindService的方式互相开启。