1.定义aidl文件
a.ITestService.aidl
package com.open.aidl.service;import com.open.aidl.service.ITestServiceCallback;interface ITestService { void registerCallback(ITestServiceCallback cb); void unregisterCallback(ITestServiceCallback cb); int request(inout Bundle mBundle);}
b.ITestServiceCallback.aidl
package com.open.aidl.service;oneway interface ITestServiceCallback { void onResponse(inout Bundle mBundle);}
2.定义Activity
3.定义Service.
package com.open.aidl.service;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.IBinder;import android.os.IInterface;import android.os.RemoteCallbackList;import android.os.RemoteException;import android.util.Log;/** * 后台服务类 * @author DexYang * */public class TestService extends Service { private final String TAG="TestService"; private Object mCallbacksLock=new Object(); private Handler mHandler=new Handler(); @Override public void onCreate() { super.onCreate();Log.v(TAG, "onCreate()"); } @Override public void onStart(Intent intent, int startId) {Log.v(TAG, "onStart()"); handleCommand(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) {Log.v(TAG, "onStartCommand()"); handleCommand(intent); return START_STICKY; } private void handleCommand(Intent intent) { } @Override public IBinder onBind(Intent arg0) {Log.v(TAG, "onBind()"); if(null!=arg0&&TestService.class.getName().equals(arg0.getAction())) { handleCommand(arg0); } return mBinder; } @Override public void onRebind(Intent intent) {Log.v(TAG, "onRebind()"); if(TestService.class.getName().equals(intent.getAction())) { handleCommand(intent); } super.onRebind(intent); } @Override public boolean onUnbind(Intent intent) {Log.v(TAG, "onUnbind()"); return true; } @Override public void onDestroy() {Log.v(TAG, "onDestroy()"); mCallbacks.kill(); android.os.Process.killProcess(android.os.Process.myPid()); super.onDestroy(); } /** * Binder 相关 */ private final CusRemoteCallbackListmCallbacks= new CusRemoteCallbackList (); private ITestService.Stub mBinder=new ITestService.Stub() { @Override public int request(Bundle mBundle) throws RemoteException {Log.v(TAG,"call from Activity request "); mHandler.postDelayed(new Runnable(){ @Override public void run() { synchronized (mCallbacksLock) { int callbacksNum = mCallbacks.beginBroadcast(); for (int i=callbacksNum-1; i>=0; i--) { try { mCallbacks.getBroadcastItem(i).onResponse(null);; } catch (Exception e) { e.printStackTrace(); } } mCallbacks.finishBroadcast(); } } }, 3000); return 0; } @Override public void registerCallback(ITestServiceCallback cb) throws RemoteException {Log.v(TAG,"registerCallback :"); if (cb != null) { mCallbacks.register(cb); } } @Override public void unregisterCallback(ITestServiceCallback cb) throws RemoteException {Log.v(TAG,"unregisterCallback :"); if (cb != null) { mCallbacks.unregister(cb); } } }; /** * 经过测试onCallbackDied()方法,只有在bindService(),没有调用unbind()方法process就挂了的情况下才会执行 * @author Administrator * @param */ private class CusRemoteCallbackList extends RemoteCallbackList { @Override public void onCallbackDied(E callback) {Log.v(TAG, "CusRemoteCallbackList onCallbackDied 1"); super.onCallbackDied(callback); } @Override public void onCallbackDied(E callback, Object cookie) {Log.v(TAG, "CusRemoteCallbackList onCallbackDied 2"); super.onCallbackDied(callback, cookie); } } }
4.配置AndroidManifest.xml文件
5.测试运行.
点击bind service , 在TestService中 打印了call from Activity,说明Activity 调用 Service 成功; 3秒后在Activity中 打印了call from service,说明Service 调用 Activity 成功。
Demo代码地址:
邮箱:zz7zz7zz@163.com
微博: