AIDL是Android进程IPC定义语言
的缩写,主要是用来定义进程间通信的接口。本文使用一个Demo程序来说明AIDL的使用,demo工程的分2个module:
- App "客户端",发送网络请求到server端,并且在接收到来自于server的数据后展示结果到UI。
- Server "服务端"负责执行耗时的网络请求。
以上2个module运行于2个独立的进程当中,进程间的通信依靠aidl接口。
Server
首先,所有的aidl接口都由server端定义,目录结构如下:
我们服务进程需要提供一个访问入口给客户进程,所以首先定义了IPostInterface.aidl,内容如下:
post方法接受一个Request请求,setResponseInterface接受一个访问客户进程的接口,该接口是用于上报网络任务的结果给客户进程的。
因为使用了自定义类Request,所以我们需要创建一个aidl文件,用于声明Request,如下 aidl文件名要和Request相同
:
接下来,还需要实现Request,Request必须实现Parcelable接口,如下:
package www.seven.com.server;
import android.os.Parcel;
import android.os.Parcelable;
/**
* @author Seven in 2017-01-07
*/
public class Request implements Parcelable {
public void setRequestId(int requestId) {
this.mRequestId = requestId;
}
public void setUrl(String url) {
this.mUrl = url;
}
public int getRequestId() {
return mRequestId;
}
public String getUrl() {
return mUrl;
}
// 请求的唯一id
private int mRequestId;
// 请求的url
private String mUrl;
protected Request(Parcel in) {
mUrl = in.readString();
mRequestId = in.readInt();
}
public Request(int reqId, String url) {
mRequestId = reqId;
mUrl = url;
}
public static final Creator<Request> CREATOR = new Creator<Request>() {
@Override
public Request createFromParcel(Parcel in) {
return new Request(in);
}
@Override
public Request[] newArray(int size) {
return new Request[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(mUrl);
parcel.writeInt(mRequestId);
}
}
上面的实现过程,都是模板代码来的,值得注意的地方是从Parcel里面读取数据的顺序,也和写入数据的顺序一致。
setResponseInterface需要提交一个aidl接口给服务进程,好让服务进程能够及时反馈网络请求的结果,改aidl接口定义如下:
该接口接受成功和失败的消息,当网络任务执行成功时,需要返回一个Response实例给客户进程,类似Request,我们也需要定义Response的aidl声明:
// Response.aidl
package www.seven.com.server;
parcelable Response;
同样,我们也需要实现Response类:
package www.seven.com.server;
import android.os.Parcel;
import android.os.Parcelable;
/**
* @author Seven in 2017-01-07
*/
public class Response implements Parcelable {
// 这个应答对应的请求。
private Request mRequest;
private int mStatusCode;
private String mResponseStr;
public Request getRequest() {
return mRequest;
}
public int getStatusCode() {
return mStatusCode;
}
public String getResponseStr() {
return mResponseStr;
}
protected Response(Parcel in) {
mRequest = in.readParcelable(getClass().getClassLoader());
mStatusCode = in.readInt();
mResponseStr = in.readString();
}
public Response(Request request, int code, String resStr) {
mRequest = request;
mStatusCode = code;
mResponseStr = resStr;
}
public static final Creator<Response> CREATOR = new Creator<Response>() {
@Override
public Response createFromParcel(Parcel in) {
return new Response(in);
}
@Override
public Response[] newArray(int size) {
return new Response[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeParcelable(mRequest, i);
parcel.writeInt(mStatusCode);
parcel.writeString(mResponseStr);
}
}
因为一个Response会保存着一个对应的Request,所以这里需要注意下载Parcelable里面读写另外一个Parcelable的实现方式。
接下来是网络任务的实现,在这里,执行网络请求,可以使用第三方库,也可以使用系统API实现,我现在只是模拟一个耗时任务而已。NetworkTask实现如下,该Task接受一个Request,并将结果提交给Handler。
package www.seven.com.server.services;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.lang.ref.WeakReference;
import www.seven.com.server.Request;
import www.seven.com.server.Response;
/**
* @author Seven in 2017-01-07
*/
public class NetworkTask extends AsyncTask<Request, Integer, Response> {
private WeakReference<Handler> mWeakRef;
public NetworkTask(Handler receiver) {
mWeakRef = new WeakReference<Handler>(receiver);
}
@Override
protected Response doInBackground(Request... requests) {
Log.d("SevenThread", "[NetworkTask] Current Thread " + Thread.currentThread().getId());
Response response = null;
if (requests[0].getRequestId() % 3 == 0) {
// 失败的情况
postFailure(requests[0].getRequestId(), requests[0].getUrl() + "'s response Fail !!!!");
} else {
// 成功的情况
response = new Response(requests[0], 200, requests[0].getUrl() + "'s response");
}
return response;
}
@Override
protected void onPostExecute(Response response) {
if (response != null) {
postSuccess(response);
}
}
private void postSuccess(Response response) {
Handler handler = mWeakRef.get();
if (handler != null) {
handler.obtainMessage(NetworkService.RESPONSE_SUCCESS, response).sendToTarget();
}
}
private void postFailure(int reqId, String msg) {
Handler handler = mWeakRef.get();
if (handler != null) {
Message message = handler.obtainMessage(NetworkService.RESPONSE_FIAL);
message.arg1 = reqId;
message.obj = msg;
message.sendToTarget();
}
}
}
该Handler来自于NetwokService,NetworkService的实现如下:
package www.seven.com.server.services;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import java.security.PublicKey;
import www.seven.com.server.IPostInterface;
import www.seven.com.server.IResponseInterface;
import www.seven.com.server.Request;
import www.seven.com.server.Response;
/**
* @author Seven in 2017-01-07
*/
public class NetworkService extends Service {
public static final int RESPONSE_SUCCESS = 0;
public static final int RESPONSE_FIAL = 1;
private IResponseInterface mIResponseInterface;
private NetworkResponseHandler mNetworkResponseHandler = new NetworkResponseHandler();
private IPostInterface mPostInterface = new IPostInterface.Stub() {
@Override
public void setResponseInterface(IResponseInterface responseInterface) throws RemoteException {
mIResponseInterface = responseInterface;
}
@Override
public boolean post(Request request) throws RemoteException {
Log.d("SevenThread", "[NetworkService] Current Thread " + Thread.currentThread().getId());
new NetworkTask(mNetworkResponseHandler).execute(request);
return true;
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mPostInterface.asBinder();
}
private class NetworkResponseHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case RESPONSE_SUCCESS:
handleSuccess((Response) msg.obj);
break;
case RESPONSE_FIAL:
handleFailure(msg.arg1, (String) msg.obj);
break;
}
}
}
private void handleSuccess(Response response) {
if (mIResponseInterface != null) {
try {
mIResponseInterface.onResponseSuccess(response.getRequest().getRequestId(), response);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
private void handleFailure(int reqId, String msg) {
if (mIResponseInterface != null) {
try {
mIResponseInterface.onResponseFailure(reqId, msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
NetworkService的具体工作是:接受一个Request,创建一个NetworkTask,执行完毕后,返回给客户进程。
Client
客户端比较简单,就一个ClientActivity,具体实现如下:
package www.seven.com.aidldemo;
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.view.View;
import android.widget.TextView;
import www.seven.com.server.IPostInterface;
import www.seven.com.server.IResponseInterface;
import www.seven.com.server.Request;
import www.seven.com.server.Response;
import www.seven.com.server.services.NetworkService;
public class ClientActivity extends AppCompatActivity {
private static int REQ_CODE = 0;
private IResponseInterface mIResponseInterface = new IResponseInterface.Stub() {
@Override
public void onResponseSuccess(final int requestId, final Response response) throws RemoteException {
runOnUiThread(new Runnable() {
@Override
public void run() {
mResponseSb.append("request " + requestId + " " + response.getResponseStr() + "\n");
mResponseTv.setText(mResponseSb.toString());
}
});
}
@Override
public void onResponseFailure(final int requestId, final String msg) throws RemoteException {
// aidl回调是在独立的线程执行,所以需要在ui线程更新
runOnUiThread(new Runnable() {
@Override
public void run() {
mResponseSb.append("request " + requestId + " " + msg + "\n");
mResponseTv.setText(mResponseSb.toString());
}
});
}
};
private IPostInterface mIPostInterface;
private StringBuilder mResponseSb = new StringBuilder();
private TextView mResponseTv;
private ServiceConnection mServiceConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
mResponseTv = (TextView) findViewById(R.id.response);
}
public void onSend(View view) {
int reqId = REQ_CODE++;
Request request = new Request(reqId, "request " + reqId);
mResponseSb.append("request " + reqId + "\n");
mResponseTv.setText(mResponseSb.toString());
if (mIPostInterface == null) {
Intent service = new Intent(this, NetworkService.class);
mServiceConnection = new ServerConnection(request);
bindService(service, mServiceConnection, Context.BIND_AUTO_CREATE);
} else {
try {
mIPostInterface.post(request);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
private class ServerConnection implements ServiceConnection {
private Request mRequest;
public ServerConnection(Request request) {
mRequest = request;
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mIPostInterface = IPostInterface.Stub.asInterface(iBinder);
try {
mIPostInterface.setResponseInterface(mIResponseInterface);
} catch (RemoteException e) {
e.printStackTrace();
}
try {
mIPostInterface.post(mRequest);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mIPostInterface = null;
mServiceConnection = null;
}
}
@Override
protected void onDestroy() {
unbindService(mServiceConnection);
super.onDestroy();
}
}
需要注意的点是:在第一次发送请求时,会先进行服务绑定;IResponseInterface接收到消息时,需要在将更新view的操作放到ui线程中,因为aidl的回调是在独立非UI线程中的。
最后,需要在manifest文件中声明NetworkService运行在独立的进程中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="www.seven.com.aidldemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="www.seven.com.aidldemo.ClientActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="www.seven.com.server.services.NetworkService"
android:process=":networking"
/>
</application>
</manifest>