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

android进程间通信AIDL的简单实现。

2017-07-27 16:26 357 查看
aidl进程间通信,肯定是两个进程之间了,我们可以简单的将其分为服务端和客户端,客户端负责发起一个求和的请求,服务端则负责执行这个求和的动作并将求和的结果返回给客户端。

先看看服务端的代码创建吧,用的开发工具是AndroidStudio,右键新建aidl文件,代码如下

interface AIDLTest {
/**
* 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);
}有个基础类型的参数说明,不包含short类型,其他的都是支持的,基础类型外,String类型也是支持的。我查了资料发现,还有charsequence和parcelable也是支持的。
aidl文件写完后需要编译一下AndroidStudio,不然拿不到aidl的引用。服务端一般就是一个Service,在onBind方法中返回一个aidl 的stub对象,然后会重写一个aidl接口中的方法,

具体的业务操作就在这里面实现了,下面贴上代码,由于比较简单,所以代码很少。

package company.huawei.com.aidltest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

/**
* Created by n911970 on 2017/7/27.
*/

public class AIDLService extends Service
{

private IBinder iBinder = new IMyAidlInterface.Stub()
{
@Override
public int getSum(int num1, int num2) throws RemoteException
{
Log.d("AIDLService","num1 = "+num1 +",num2 =" +num2);
return num1 + num2;
}
};

@Override
public IBinder onBind(Intent intent)
{
return iBinder;
}
}注意Service需要在Manifest中注册,exported属性需要置为true
<service
android:name=".AIDLService"
android:enabled="true"
android:exported="true">

</service>下面看下客户端的代码,主要就是绑定服务端的这个Service然后获取IBinder对象,然后进行求和的请求操作。客户端也需要写一个aidl文件,必须和服务端的完全一致,包括包名和aidl文件名,不然会报错。如下图所示



下面看下客户端的代码吧,写了个Buttton,点击这个button进行求和的操作,然后在服务端打了log,客户端弹出toast,已亲自试验过是可以的了。

直接上代码吧,有简单的注释。

package company.huawei.com.aidlclient;

import company.huawei.com.aidltest.IMyAidlInterface;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

private static final String TAG = "MainActivity";
private IMyAidlInterface aidl;

private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

Log.d(TAG,"---onServiceConnected---");
aidl = IMyAidlInterface.Stub.asInterface(iBinder);

}

@Override
public void onServiceDisconnected(ComponentName componentName) {

aidl = null;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
getSum(2, 4);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
bindService();
}

/**
* 绑定service
*/
private void bindService() {
Intent intent = new Intent();
//注意必须为显式的方式去启动这个service
intent.setComponent(new ComponentName("company.huawei.com.aidltest", "company.huawei.com.aidltest.AIDLService"));
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}

/**
* 求和操作
*
* @param a 参数1
* @param b 参数2
*/
private void getSum(int a, int b) throws RemoteException {
int sum = aidl.getSum(a, b);
Log.d(TAG, "sum =
4000
" + sum);
Toast.makeText(MainActivity.this, "求和后的结果为:" + sum, Toast.LENGTH_LONG).show();
}

@Override
protected void onDestroy() {
super.onDestroy();
//页面销毁记得解绑service
unbindService(conn);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息