博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[置顶] android AIDL 进程间通信
阅读量:5765 次
发布时间:2019-06-18

本文共 4210 字,大约阅读时间需要 14 分钟。

 

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 CusRemoteCallbackList
mCallbacks= 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

微博:
你可能感兴趣的文章
006_mac osx 应用跨屏幕
查看>>
nginx中配置文件的讲解
查看>>
MindNode使用
查看>>
SQL Server 2016 Alwayson新增功能
查看>>
HTTP库Axios
查看>>
CentOS7下安装python-pip
查看>>
认知计算 Cognitive Computing
查看>>
左手坐标系和右手坐标系 ZZ
查看>>
陀螺仪主要性能指标
查看>>
Java 架构师眼中的 HTTP 协议
查看>>
Linux 目录结构和常用命令
查看>>
Linux内存管理之mmap详解 (可用于android底层内存调试)
查看>>
利润表(年末)未分配利润公式备份
查看>>
Android开发中ViewStub的应用方法
查看>>
gen already exists but is not a source folder. Convert to a source folder or rename it 的解决办法...
查看>>
HDOJ-2069Coin Change(母函数加强)
查看>>
遍历Map的四种方法
查看>>
IOS atomic与nonatomic,assign,copy与retain的定义和区别
查看>>
JAVA学习:maven开发环境快速搭建
查看>>
Altium Designer 小记
查看>>