這幾天搞了搞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<icallback> mCallbacks = new RemoteCallbackList<icallback>();
- 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);
- }
- };
- }
- </icallback></icallback>
復制代碼
然後是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標記:
- <service android:name=".service.MainService">
- </service>
復制代碼
運行程序,查看Logcat
結果如下:
總結:
通過aidl總算實現了Service與Activity的通信,寫起來麻煩點,使用諸如ContentProvider,Broadcast等也可以實現.
這樣做很像是在使用MVC設計模式(Activity負責VIEw,Service以及其他類負責 Model,aidl(ServiceConnection)負責Controller),其實我對於aidl也是一知半解- -,明天研究下如何讓activity去調用service中的方法.