您的位置:首页 > 移动开发 > Android开发

Android 双进程守护(分分钟实现)

2016-10-09 17:27 183 查看
是这个情况,前几天在腾讯公开课看到的,确实不错,首先我先说下怎么实现的?

**实现思路:**创建A,B两个服务,然后同时启动A、B服务,双进程守护,当然要另开一个进程,通过AIDL实现跨进程通信,在A、B服务中的onStartCommand方法中,互相绑定对方,然后我们想想,服务如果被强杀了怎么办,接下来是关键:我们实现ServiceConnection这个接口,在onServiceDisconnected方法中重新绑定并且start服务(被kill的service)这样每次killservice都会走到这个方法里面,哈哈,服务就被重启了,MDZZ很强势有没有?还有谁?我就想问下。

对了还有web视频要看,先不吹B,直接上代码:


本地服务code:

package app.project.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.util.Log;
import android.widget.Toast;

import app.project.IMyAidlInterface;

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, " 本地服务started", 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) {
}

@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(LocalService.this, "远程服务killed", 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);

}
}


远程服务code:

package app.project.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.util.Log;
import android.widget.Toast;

import app.project.IMyAidlInterface;

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, " 远程服务started", 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) {

}

@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(RomoteService.this, "本地服务killed", 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);

}
}


aidl文件:

// IMyAidlInterface.aidl
package app.project;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
String getServiceName();
}


这里说下aidl的创建,右键aidl文件,创建完成后看图:



注意啦:一定要clean下项目,保证在gen下生成aidl文件,看图:



配置文件:


<service
android:name=".service.LocalService"
android:enabled="true"
android:exported="true" />
<service
android:name=".service.RomoteService"
android:process=":romoteservice"
android:enabled="true"
android:exported="true"></service>


接下来就是在activity里启动A、B服务咯,测试就看你自己咯!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: