- 浏览: 153620 次
- 性别:
- 来自: 深圳
最新评论
-
dawei1980:
请问,解压密码是多少?
Android本地APP集成Mui框架 -
牧羊之人:
Android本地APP集成Mui框架 -
还有也许:
貌似懂了一点。如果onCreate方法中创建了一个db,然后在 ...
Android线程模式(handler,thread,looper) -
chenshijun0101:
header里面怎么更改他的样式呢?急求
android Preference相关样式修改 -
qlraishui:
good
Binder机制分析【三】-service绑定Binder
前一章说了下Binder机制的实现分析,这章就主要通过实际的例子来看看,具体的服务端创建。一般而言,同一个进程里的沟通称之为短程沟通,进程间的沟通称之为远程沟通,短程沟通的效率远高于远程沟通,但是Android提供的Binder机制就提供了高效率的远程沟通。
下面通过Activity与Service之间的沟通为例,列举出2种实现方式。
例子一:通过直接继承Binder实现
1. 实现Binder类
该类继承了Binder类,就必须实现onTransact方法,与客户端的transact方法相对应。其中参数code代表客户端调用码,决定在服务器端调用哪个方法,和transact端的保持一致,data是请求参数,由客户端设置,reply为返回参数,服务器端设置。Flags表示ipc通信方式。0表示双向,1表示单向。
2. 实现service类
Service相对简单,主要是初始化Binder对象,并且在onBind中返回。
3. Activity与service之间的沟通
首先需要创建ServiceConnection对象,并且在bind时返回Binder对象
其次,调用bindService绑定服务。
最后,在需要调用服务器端方法的地方,通过transact进行沟通,这里传递的参数必须是Parcel的。
由此可见,这种方法提供的沟通手段比较单一,只能通过transact进行沟通,如果服务器端的接口很多,这样调用起来不太方便,所以Android提供了一种更为简单的方式来实现-AIDL。
AIDL定义了Proxy/Stub封装了IBinder接口,提供了更加方便的新接口。
例子二:通过AIDL实现
1. 单一aidl文件接口
该接口文件以aidl结尾,中间封装了2个方法,该文件生成后,会在gen目录下自动生成对应的java文件,里面包含了具体的类别继承,反向呼叫等实现。
2. 实现Binder类
现在的实现继承接口改为了aidl类文件的Stub,此处需要实现接口中的2个接口方法
3. 实现service类
此处同例一保持一致
4. activity与service之间的沟通
首先,需要实现ServiceConnection对象,注意返回binder对象时与例一之间的差别。
其次,需要绑定服务,方法同例一
最后,具体调用服务器端接口
总结:
2种方法都可以实现跨进程通信,但是aidl实现相对简单,接口直接调用。
问题:
1.如果我们想保持和 Service 的通信,又不想让 Service 随着 Activity 退出而退出呢?你可以先 startService() 然后再 bindService() 。当你不需要绑定的时候就执行 unbindService() 方法,执行这个方法只会触发 Service 的 onUnbind() 而不会把这个 Service 销毁。这样就可以既保持和 Service 的通信,也不会随着 Activity 销毁而销毁了。
2.Activity has leaked ServiceConnection异常
这个异常是由于在启动应用时绑定了service,退出时没有接触绑定导致,可以在退出时调用unbindService()方法解决。
3.在先startService(),然后bindService()的情况下,在退出时
1).如果只调用unbindService(),在第一次会调用onUnbind,然后再次绑定时,通过start启动,再退出是不会调用onUnbind。
2).如果调用unbindService()和stopService,整个service会调用ondestory进行销毁。
3.在通过MediaPlayer播放音乐时,网上都说如果在activity中播放,在activity退出时,音乐播放会停止,这个纯粹是他妈的扯淡,我以前还对此坚信不疑。MediaPlayer播放音乐,会重新启动一个service进行播放,如果你的activity或service已经destory了,是不影响播放service的生命周期的。
例子源码见附件
本章介绍了Binder服务器的实现及举例说明了binder客户端与服务端的交互,下一章介绍下Service是如何绑定Binder的
这个是不一样的,aidl其实默认的也是0.
oneway的意思是在ipc过程中,如果调用者和被调用者在2个不同的进程,调用者不必等到被调用者返回就继续执行。
以下是引用:The oneway keyword means that if that call results in an IPC (i.e. the caller and callee are in different processes) then the calling process will not wait for the called process to handle the IPC. If it does not result in an IPC (i.e. they're both in the same process), the call will be synchronous. It's an unfortunate detail that simplifies the implementation of binder IPC a lot. If they're in the same process, the call is just a regular java method call.
下面通过Activity与Service之间的沟通为例,列举出2种实现方式。
例子一:通过直接继承Binder实现
1. 实现Binder类
public class MyBinder extends Binder { private Context mContext; public MyBinder(Context context){ mContext = context; } @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { reply.writeString(data.readString()+" mp3"); switch(code){ case 1: MyMediaPlayer.getIntance(mContext).play(); break; case 2: MyMediaPlayer.getIntance(mContext).stop(); break; default: break; } return true; } }
该类继承了Binder类,就必须实现onTransact方法,与客户端的transact方法相对应。其中参数code代表客户端调用码,决定在服务器端调用哪个方法,和transact端的保持一致,data是请求参数,由客户端设置,reply为返回参数,服务器端设置。Flags表示ipc通信方式。0表示双向,1表示单向。
2. 实现service类
public class MyService extends Service { private Binder mBinder = null; @Override public IBinder onBind(Intent arg0) { Log.e("XXX", "onBind"); return mBinder; } @Override public void onCreate() { super.onCreate(); mBinder = new MyBinder(this); Log.e("XXX", "onCreate"); } }
Service相对简单,主要是初始化Binder对象,并且在onBind中返回。
3. Activity与service之间的沟通
首先需要创建ServiceConnection对象,并且在bind时返回Binder对象
private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.e("XXX", "onServiceDisconnected"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { mIBinder = service; Log.e("XXX", "onServiceConnected"); } };
其次,调用bindService绑定服务。
Intent intent = new Intent("com.eric.ipc.binder"); bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
最后,在需要调用服务器端方法的地方,通过transact进行沟通,这里传递的参数必须是Parcel的。
Parcel send_data = Parcel.obtain(); send_data.writeString("play"); Parcel reply_data = Parcel.obtain(); mIBinder.transact(1, send_data, reply_data, 0);
由此可见,这种方法提供的沟通手段比较单一,只能通过transact进行沟通,如果服务器端的接口很多,这样调用起来不太方便,所以Android提供了一种更为简单的方式来实现-AIDL。
AIDL定义了Proxy/Stub封装了IBinder接口,提供了更加方便的新接口。
例子二:通过AIDL实现
1. 单一aidl文件接口
package com.eric.ipc.aidl; interface IMediaPlayer { void play(); void stop(); }
该接口文件以aidl结尾,中间封装了2个方法,该文件生成后,会在gen目录下自动生成对应的java文件,里面包含了具体的类别继承,反向呼叫等实现。
2. 实现Binder类
public class MyBinder extends IMediaPlayer.Stub { private Context mContext; public MyBinder(Context context){ mContext = context; } @Override public void play() throws RemoteException { MyMediaPlayer.getIntance(mContext).play(); } @Override public void stop() throws RemoteException { MyMediaPlayer.getIntance(mContext).stop(); } }
现在的实现继承接口改为了aidl类文件的Stub,此处需要实现接口中的2个接口方法
3. 实现service类
此处同例一保持一致
4. activity与service之间的沟通
首先,需要实现ServiceConnection对象,注意返回binder对象时与例一之间的差别。
private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.e("XXX", "onServiceDisconnected"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { mIBinder = IMediaPlayer.Stub.asInterface(service); Log.e("XXX", "onServiceConnected"); } };
其次,需要绑定服务,方法同例一
最后,具体调用服务器端接口
mIBinder.play();
总结:
2种方法都可以实现跨进程通信,但是aidl实现相对简单,接口直接调用。
问题:
1.如果我们想保持和 Service 的通信,又不想让 Service 随着 Activity 退出而退出呢?你可以先 startService() 然后再 bindService() 。当你不需要绑定的时候就执行 unbindService() 方法,执行这个方法只会触发 Service 的 onUnbind() 而不会把这个 Service 销毁。这样就可以既保持和 Service 的通信,也不会随着 Activity 销毁而销毁了。
2.Activity has leaked ServiceConnection异常
这个异常是由于在启动应用时绑定了service,退出时没有接触绑定导致,可以在退出时调用unbindService()方法解决。
3.在先startService(),然后bindService()的情况下,在退出时
1).如果只调用unbindService(),在第一次会调用onUnbind,然后再次绑定时,通过start启动,再退出是不会调用onUnbind。
2).如果调用unbindService()和stopService,整个service会调用ondestory进行销毁。
3.在通过MediaPlayer播放音乐时,网上都说如果在activity中播放,在activity退出时,音乐播放会停止,这个纯粹是他妈的扯淡,我以前还对此坚信不疑。MediaPlayer播放音乐,会重新启动一个service进行播放,如果你的activity或service已经destory了,是不影响播放service的生命周期的。
例子源码见附件
本章介绍了Binder服务器的实现及举例说明了binder客户端与服务端的交互,下一章介绍下Service是如何绑定Binder的
- IPC_Achieve.zip (25.2 KB)
- 描述: 例子源码
- 下载次数: 77
评论
2 楼
zhy20045923
2012-03-13
nanapoleon 写道
Flags表示ipc通信方式。0表示双向,1表示单向。 这个和aidl的oneway是不是一样的。
这个是不一样的,aidl其实默认的也是0.
oneway的意思是在ipc过程中,如果调用者和被调用者在2个不同的进程,调用者不必等到被调用者返回就继续执行。
以下是引用:The oneway keyword means that if that call results in an IPC (i.e. the caller and callee are in different processes) then the calling process will not wait for the called process to handle the IPC. If it does not result in an IPC (i.e. they're both in the same process), the call will be synchronous. It's an unfortunate detail that simplifies the implementation of binder IPC a lot. If they're in the same process, the call is just a regular java method call.
1 楼
nanapoleon
2012-03-13
Flags表示ipc通信方式。0表示双向,1表示单向。 这个和aidl的oneway是不是一样的。
发表评论
-
判断图片是浅色还是深色
2020-03-04 13:23 726首先需要获取 WallpaperManager.FLAG_L ... -
如何将uri转成真实路径地址
2018-10-15 17:38 1129/** * 获取文件选择器选中的文 ... -
备用网址记录
2018-01-08 11:05 400各种开源下载 http://www.mvnjar.com/ ... -
android中touch事件,click事件,longclick事件分析
2016-08-03 15:51 1439针对屏幕上的一个View控件,Android如何区分应当触发o ... -
Android 快速开发系列 打造万能的ListView GridView 适配器
2016-06-27 17:21 637前往 http://blog.csdn.net/lmj6235 ... -
android中的Handler和AsyncTask如何防止内存泄露
2016-06-13 13:55 1586Handler泄露的关键点有两个: 1). 内部类 ... -
[转载]SharedPreferences 存储java对象,很实用
2016-04-14 16:36 1015public void putObject(String ke ... -
Android本地APP集成Mui框架
2016-01-26 14:41 30442.如何在安卓原生APP中 ... -
Android与设计模式浅谈
2015-04-27 10:42 1022Android作为新一代的操作系统,集合着Google ... -
从网页启动Activity
2015-03-24 11:28 1313正好Android SDK 给我们提供了解决方案,在网页中点击 ... -
[转]android shape的使用
2014-10-13 13:30 726shape用于设定形状,可以在selector,layout等 ... -
touch事件分发处理流程
2014-05-23 09:44 792Touch 事件发生时 Activity 的 dispatch ... -
修改标准GSensor相关,是重力感应游戏在平板都可以玩
2013-12-21 11:27 812为什么有些重力感应的游戏不能玩,有些可以玩,主要原因在于fra ... -
Android模拟按键
2013-10-14 14:27 2358如果想要实现类似iphone的悬浮框按钮,那就必须知道如何去模 ... -
android资源适配解析及资源适配优先级规则
2013-10-12 12:41 36431.sw的值是怎么计算得来 ... -
[转]隐藏虚拟按键(导航栏)的方法
2013-10-12 10:35 2859Controls for system UI visibili ... -
【转】解决Android与服务器交互大容量数据问题
2013-09-02 14:40 2007对于目前的状况来说, ... -
[转载]Android大图裁剪解决办法
2013-04-25 14:29 2057cropimage 可以调用手机自带的com.android ... -
反锯齿办法
2012-12-28 14:14 915在Android中,目前,我知道有两种出现锯齿的情况。 ① ... -
android线程的那些事
2012-11-17 15:36 2259有些时候Thread里面更新UI是可以成功的。 比如在Acti ...
相关推荐
CogNex Binder1 - VP6 - Standard
需要在系统源码下编译,或者提取出对应的头文件亦可。这里需要注意Android4.x以后系统SeLinux如果打开,系统级需要配置对应的sepolicy才能使用。测试阶段推荐直接setenforce 0关闭鉴权即可
Binder面试系列00-binder爱情例子.md
Binder1 - VP6 - Advanced.pdf
Binder-swagger-java 的功能很简单,就是帮助构建 swagger.json 所对应的 swagger 对象,并让它可以被 swagger ui 等客户端访问。 注:binder-swagger-java 直接基于 form-binder-java / swagger-models...
本文档详细解说了从MediaPlayerService的创建,到其Binder机制的实现,BnMediaPlayerService与BpMediaPlayerService之间的关系,Binder客户端如何与服务端进行通信。希望对你们有所帮助。
android系统深入浅出binder机制分析
cloud-sleuth-master/spring-cloud-security-master/spring-cloud-cluster-master/spring-cloud-stream-master/ribbon-master/zuul-master/eureka-master/Hystrix-master/feign-master/pring-cloud-stream-binder-...
Android Binder机制 总共11篇博客。详细介绍BINDER的内部机制
我们这里将以MediaService的例子来分析Binder的使用:l ServiceManager,这是Android OS的整个服务的管理程序l MediaService,这个程序里边注册了提供媒体播放的服务程序MediaPlayerService,我们最后只分析这个l ...
android2.3源码-binder机制.doc 对binder机制的详细分析
atlassian-xmlrpc-binder-support-0.7.jar
通过图解的方式,将Android Binder通信的根本原理呈现出来。
atlassian-xmlrpc-binder-server-spring-0.8.2.jar
atlassian-xmlrpc-binder-server-spring-0.11.jar
atlassian-xmlrpc-binder-server-spring-0.9.jar
atlassian-xmlrpc-binder-server-spring-0.8.1.jar
atlassian-xmlrpc-binder-server-spring-0.7.jar
Android进程间通信-Binder机制详解