安卓进程间AIDL通信

下面来介绍下安卓不同进程之间通信方式之一AIDL
1:客户端和服务器端都要有包名及内容相同的AIDL文件
例如:
package a.b.c;
interface IMyAidlInterface {
boolean connectCarplayProfile (String device);
}

2:服务器端代码

服务器端需要一个Service,来让客户端绑定。
客户端绑定成功后,返回AIDL binder 对象给客户端。
让其可以使用服务端提供的AIDL 接口。
服务器端Service 实现如下:

服务器端需要一个类来继承AIDL 文件自动生成的.Stub类
例如:

package a.b.c;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
public class ServiceTestStub extends IMyAidlInterface.Stub //IMyAidlInterface为aidl 文件接口名
{
private static final String cTag = “ServiceTestStub”;
private volatile static ServiceTestStub instance;
private Context mContext;
private ServiceTestStub(Context context) {
mContext = context;
}
public static ServiceTestStub getInstance(Context mContext) {
if (instance == null) {
synchronized (ServiceTestStub.class) {
if (instance == null) {
instance = new ServiceTestStub(mContext);
}
}
}
return instance;
}
@Override
public boolean connectCarplayProfile(String device) throws RemoteException {
//编写具体接口实现
return true;
}
}

package a.b.c.service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import a.b.c.ServiceTestStub;//实现AIDL接口的类
public class ServiceTest extends Service {
private static final String Tag = “ServiceTest”;
private Context context;
private IBinder serviceTestStub;
@Override
public void onCreate() {
context = this;
super.onCreate();
NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(“1”, “xxxxxxxxxxxxxxx”, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
Log.d(Tag," -----> onCreate");
//服务端Service 启动,获取继承自动生成AIDL接口的类的实例
serviceTestStub = ServiceTestStub.getInstance(context);
}
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
Log.d(Tag," -----> onStartCommand");
return Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(Tag," -----> onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
Log.d(Tag," -----> onBind");
return serviceTestStub;//客户端绑定成功返回的IBinder 对象为实现AIDL接口类的对象
}
}

3:客户器端代码
客户端需要绑定服务器端
由于Service有异常情况,绑定服务成功后,最好追加注册死亡回调
一旦Service断开,可以尝试连接服务端
此例追加一个测试Button,按下调用AIDL 接口
例如:

package xx.xx.xx;
import android.app.Notification;
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.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;

import a.b.c.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
private static IMyAidlInterface carplayIn;
private Context mContext;
private Button uu ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this.getApplicationContext();
setContentView(R.layout.activity_main);
uu = findViewById(R.id.yy_test);
uu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
connectCarplayProfile(“11111”);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
//注意安卓9.0 启动Service方式与之前有所不同,
Intent intent = new Intent();
intent.setAction(“a.b.c.ServiceTest”);
intent.setComponent(new ComponentName(“a.b.c”,“a.b.c.service.ServiceTest”));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Notification notification = new Notification.Builder(mContext, “1”).build();
if (mContext instanceof Service) {
((Service) mContext).startForeground(1, notification);
}
}else{
mContext.startService(intent);
}
//服务器端Service绑定成功或者断开会回调carplayserviceconnection函数
mContext.bindService(intent, carplayserviceconnection, Context.BIND_AUTO_CREATE);
}
public boolean connectCarplayProfile(String device) throws RemoteException {
carplayIn.connectCarplayProfile(device);
return true;
}
public static IMyAidlInterface getCarplayService() {
return carplayIn;
}
ServiceConnection carplayserviceconnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (service != null) {
try {
服务绑定成功后获取到AIDL对象
carplayIn = IMyAidlInterface.Stub.asInterface(service);
if (carplayIn != null) {
//注册Service死亡回调
service.linkToDeath(recipient, 0);
}
} catch (Exception e) {
}
}
}
};
private IBinder.DeathRecipient recipient = new IBinder.DeathRecipient() {
@Override
public void binderDied() {
carplayIn.asBinder().unlinkToDeath(recipient, 0);
carplayIn = null;
for (int counter = 1; counter <= 10; counter++) {
try {
if (carplayIn != null) {
return;
}
Thread.sleep(500 * counter);
Intent intent = new Intent();
intent.setAction(“a.b.c.ServiceTest”);
intent.setComponent(new ComponentName(“a.b.c”,“a.b.c.service.ServiceTest”));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Notification notification = new Notification.Builder(mContext, “1”).build();
if (mContext instanceof Service) {
((Service) mContext).startForeground(1, notification);
}
}else{
mContext.startService(intent);
}
mContext.bindService(intent, carplayserviceconnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值