android双服务保活,Android双进程保活机制示例

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线程

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文件中加入配置,让该服务成为一个新的线程

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的方式互相开启。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值