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

Android AIDL的理解和使用

2015-12-23 13:00 471 查看
注:内容部分来自于极客学院

一、AIDL的使用场景

1.AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参数。

2.只有当你允许其他的一个应用程序访问你的应用程序的服务时,

服务端程序使用Intent显示的启动另一个App的Service

1.在服务端程序要有一个Service

public class AppService extends Service {
public AppService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();
Log.e("tag","Service is create");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.e("tag", "Service is destory");
}


2.在客户端程序中

private Intent serviceIntent;
Button btn_start;
Button btn_stop;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start = (Button) findViewById(R.id.btn_start);
btn_stop = (Button) findViewById(R.id.btn_stop);
serviceIntent = new Intent();
//通过包名显示的启动一个Service    第一个参数为app的包名  第二个为app中Service的全路径
serviceIntent.setComponent(new ComponentName("com.tjpld.aidlservicetest", "com.tjpld.aidlservicetest.AppService"));
btn_start.setOnClickListener(this);
btn_stop.setOnClickListener(this);
}


使用AIDL绑定方式启动一个Service

1.首先创建一个AIDL文件

/*
*AIDL测试
*/
package com.tjpld.aidlservicetest;

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

interface IAidlAppService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);

}


2.在Service的Bind中返回创建的AIDLService对象

@Override
public IBinder onBind(Intent intent) {
//通过AIDL绑定一个Service
return new IAidlAppService.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}
};
}


3.在客户端启动用绑定的方式启动一个Service

bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);


三、通过AIDL绑定Service并通信

1.在你的AIDL文件中添加一个回调方法

interface IAidlAppService {
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
void setData(String data);
}


2.在Servcie中实现该方法

String PACKAGE_SAYHI="com.example.test"
String  data="内部消息";
boolean isRunning;
public AppService() {
}

@Override
public IBinder onBind(Intent intent) {
//通过AIDL绑定一个Service
return new IAidlAppService.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}

@Override
public void setData(String data) throws RemoteException {
AppService.this.data=data;
}
//在这里可以做权限认证,return false意味着客户端的调用就会失败,比如下面,只允许包名为com.example.test的客户端通过,
//其他apk将无法完成调用过程
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws RemoteException {
String packageName = null;
String[]packages=MyService.this.getPackageManager(). getPackagesForUid(getCallingUid());
if (packages != null && packages.length > 0) {
packageName = packages[0];
}

if (!PACKAGE_SAYHI.equals(packageName)) {
return false;
}

return super.onTransact(code, data, reply, flags);
};
}


3.在Client端建立一个Folder类型为AIDL

4.将服务端保存AIDL文件的包名原封不动的复制,并且在client的AIDL文件夹下建立相同的包,并考入AIDL文件

5.实例化AIDL对象在onServiceConnection

private IAidlAppService iAidlService = null;//远程通信服务
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("tag", "BindService Success");
//注意绑定的方式
iAidlService = IAidlAppService.Stub.asInterface(service);
}
//-----------------------------
case R.id.btn_send:
//执行远程通信
if (iAidlService != null) {
try {
Log.e("tag", "data---->" + et_data.getText().toString());
iAidlService.setData(et_data.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
break;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: