StupidBeauty
Read times:591Posted at:Thu Feb 14 21:07:23 2013
- no title specified

转载 Android Service 之三(Bind Service, 继承自 Binder 类)

  • 在同一个应用程序内部, 要想使用 由Service提供的服务的话, 可通过 Binder来绑定到该Service对象,然后通过绑定过程 中得到的IBinder对象 来调用 该Service对象的公有方法

http://rainbow702.iteye.com/blog/1144521

亮点

第一种:继承自 Binder 类

需要注意的是,这种方式仅仅适用于这种场合:service 与 application 在同一个进程中。这种场合也是最最常见的。

它分以下几个步骤:

a. 在你的 service 类中声明一个内部类来继承 Binder 类。在该内部类中,最好提供一个公共方法来返回你的 service 实例。

b. 在你的 service 类中需要声明一个这个内部类的实例,以供在 onBind() 方法中返回

c. 在 client 端,在 onServiceConnected() 方法中得到从 onBind() 方法中返回的 IBinder 对象,然后可以通过该 对象中的公共方法得到相应的 service 实例,正如 第一个步骤 所说的那样。

d. 在 service 中提供公共方法, 这样就可以在组件(如 Activity 中调用这些公共方法了)

少女时代

Your opinions
Your name:Email:Website url:Opinion content:
- no title specified

HxLauncher: Launch Android applications by voice commands

 
Recent comments
2017年4月~2019年4月垃圾短信排行榜Posted at:Thu Sep 26 04:51:48 2024
Qt5.7文档翻译:QWebEngineCookieStore类,QWebEngineCookieStore ClassPosted at:Fri Aug 11 06:50:35 2023盲盒kill -9 18289 Grebe.20230517.211749.552.mp4