本文主要介绍怎么实现AIDL service并且添加回调。
1.需要定义两个AIDL interface
ITestService为测试的service, ITestCallback 为回调的结果。
// ITestService.aidl
package com.htc.soundrecorder;
import com.htc.soundrecorder.ITestCallback;
// Declare any non-default types here with import statements
interface ITestService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void testCase(ITestCallback testCallback);
}
// ITestCallback.aidl
package com.htc.soundrecorder;
// Declare any non-default types here with import statements
interface ITestCallback {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void onResult(int result);
}
创建好之后,Android Studio会自动创建对应的Java 文件。我们不需要理会。
我们的下一步是创建对应的Service。
TestService.java
/**
* Created by fredwang on 15-9-22.
*/
public class TestService extends Service{
private static final String TAG = "TestService";
private ITestCallback mTestCallback;
@Override
public IBinder onBind(Intent intent) {
//Activity bindService之后会得到这个mBinder
return mBinder;
}
//实现ITestService,mBinder是onBind中返回的
private final ITestService.Stub mBinder = new ITestService.Stub()
{
@Override
//处理真正要做的事情
public void testCase(ITestCallback testCallback) throws RemoteException {
mTestCallback = testCallback;
}
};
}
ITestCallback 只是用做回调,不需要和Activity交互,所以不用单独一个class处理,只需要在需要回调的class中实现以下代码即可:
private final ITestCallback.Stub handleTestCallbackup = new ITestCallback.Stub() {
@Override
public void onResult(int result) throws RemoteException {
VRLog.d(TAG, " onResult for ITestCallback ");
switch (result) {
case Constants.TEST_OK:
VRLog.d(TAG, " ITestCallback TEST_OK " );
unbindTestService();
break;
case Constants.TEST_FAIL:
VRLog.d(TAG, " ITestCallback TEST_FAIL " );
unbindTestService();
break;
}
}
};
Constants.TEST_OK回调数据在TestService中如下操作:
AsyncTask的三个参数分别传递给doInBackground, OnProgressUpdate,onPostExecute
private class testTask extends AsyncTask<Void, Void, Boolean>
{
@Override
//真正的耗时操作在这里进行。并将结果给onPostExecute
protected Boolean doInBackground(Void... params) {
boolean result = true;
return result;
}
@Override
//耗时操作之前的准备工作
protected void onPreExecute() {
super.onPreExecute();
}
@Override
//获得result之后的处理
protected void onPostExecute(Boolean result) {
if (result) {
try {
mTestCallback.onResult(Constants.TEST_OK);
} catch (RemoteException e) {
e.printStackTrace();
}
} else {
try {
mTestCallback.onResult(Constants.TEST_FAIL);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
这样当Activity收到回调结果就可以处理Service的unbind操作。
接下来需要做的是在Activity 绑定TestService并将回调传进去。
private void bindTestService() {
if (null == mTestServiceConnection) {
mTestServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//此处的service 就是之前onBinder返回的结果
mTestService = ITestService.Stub.asInterface(service);
try {
if (mTestService != null) {
//设置回调,处理真正的操作
mTestService.testCase(handleTestCallbackup);
}
} catch (RemoteException e){
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
VRLog.d(TAG, " onServiceConnected ");
mTestService = null;
}
};
Intent intent = new Intent();
intent.setClass(this, TestService.class);
startService(intent);
//真正绑定TestService,并启动
if (!bindService(intent, mTestServiceConnection,
Context.BIND_AUTO_CREATE)) {
}
}
}
还要处理Service断开后的unbind
private void unbindTestService() {
VRLog.d(TAG, "enter bindTestService ");
if (null != mTestServiceConnection) {
unbindService(mTestServiceConnection);
mTestServiceConnection = null;
}
}
至此,所有的工作都完成,只需要在Activity适当的地方调用bindTestService即可。