使用android中的AIDL让Service与Activity通信
这几天搞了搞AIDL,终于弄明白了在AIDL中让Service调用Activity的方法
代码:
首先建立一个ICallback.aidl文件,作为Activity中的回调方法
package com.zhang.test.service;
interface ICallback {
void showResult(int result);
}
然后再建立一个IService.aidl用来在Activity中接收Service回调,以及在Service中onBind时返回的Binder
注意:aidl中import不能写com.xxx.*,要写全类的路径
package com.zhang.test.service;
import com.zhang.test.service.ICallback;
interface IService {
void registerCallback(ICallback cb);
void unregisterCallback(ICallback cb);
}
接下来是service:
MainService.java
package com.zhang.test.service;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.Log;
public class MainService extends Service {
private static final String TAG = "MainService";
private RemoteCallbackList mCallbacks = new RemoteCallbackList();
private IService.Stub mBinder = new IService.Stub() {
@Override
public void unregisterCallback(ICallback cb){
if(cb != null) {
mCallbacks.unregister(cb);
}
}
@Override
public void registerCallback(ICallback cb){
if(cb != null) {
mCallbacks.register(cb);
}
}
};
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
return mBinder;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
//这里不知道为什么,直接使用callback方法回调showResult
//mCallbacks.beginBroadcast()是0,需要用handler延迟1000毫秒
//也许是在activity中binService太耗时的原因?
mHandler.sendEmptyMessageDelayed(0, 1000);
super.onCreate();
}
@Override
public void onDestroy() {
mHandler.removeMessages(0);
mCallbacks.kill();
super.onDestroy();
}
private void callBack() {
int N = mCallbacks.beginBroadcast();
try {
for (int i = 0; i < N; i++) {
mCallbacks.getBroadcastItem(i).showResult(i);
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
mCallbacks.finishBroadcast();
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
callBack();
super.handleMessage(msg);
}
};
}
然后是activity:
package com.zhang.test;
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 com.zhang.test.service.ICallback;
import com.zhang.test.service.IService;
import com.zhang.test.service.MainService;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private IService mService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this, MainService.class);
bindService(i, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
if(mService!=null){
try {
mService.unregisterCallback(mCallback);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
}
//destroy的时候不要忘记unbindService
unbindService(mConnection);
super.onDestroy();
}
/**
* service的回调方法
*/
private ICallback.Stub mCallback = new ICallback.Stub() {
@Override
public void showResult(int result) {
Log.d(TAG, " result : " + result);
}
};
/**
* 注册connection
*/
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected");
mService = IService.Stub.asInterface(service);
try {
mService.registerCallback(mCallback);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
}
};
}
最后不要忘记在manifest中加上service标记:
总结:
通过aidl总算实现了Service与Activity的通信,写起来麻烦点,使用诸如ContentProvider,Broadcast等也可以实现.
这样做很像是在使用MVC设计模式(Activity负责View,Service以及其他类负责Model,aidl(ServiceConnection)负责Controller),其实我对于aidl也是一知半解- -,明天研究下如何让activity去调用service中的方法.
|