Android 本地服务示例、bindservice() 和 ServiceConnection()
我有一个与 @mnish 大约一年前提出的问题相关的问题。
请看看他的问题和代码。他实现了一个ServiceConnection()并将其传递给bindService()。这遵循顶部附近的 Service 文档中的本地服务示例。
我想实现本地服务示例,因此我尝试添加 @mnish 问题/答案中的一些详细信息。在 ServiceConnection() 中 @mnish 的这一行让我感到困惑:
mService = ILocService.Stub.asInterface(iservice);
我理解 @mnish 写了这段代码,但是有人知道 ILocService 是什么以及我如何创建自己的 ILocService 吗?这个构造记录在哪里?我需要它吗?另外IBinder iservice的价值从何而来?
I have a question which is related to this question that was asked by @mnish about a year ago.
Please have a look at his question and code. He implements a ServiceConnection() and passes it to bindService(). This follows the Local Service Sample in the Service documentation near the top.
I want to implement the Local Service Sample, so I am trying to add some details from @mnish question/answer. In ServiceConnection() @mnish has this line that confuses me:
mService = ILocService.Stub.asInterface(iservice);
I understand @mnish wrote this code, but does anybody have any idea what ILocService is and any idea how I might go about creating my own ILocService? Where is this construct documented and do I need it? Also where does the value for IBinder iservice come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
他可能正在使用 Android 接口定义语言 (AIDL)
http://developer.android.com/guide/components/aidl.html
因此,他必须使用服务器端实现的存根,如记录的那样:
iservice 引用来自 onServiceConnected 方法,该方法在将服务绑定到您的活动后调用。调用bindService 会传递实现onServiceConnected 方法的ServiceConnection。
当服务的实现是本地的时,您不需要“IRemoteService.Stub.asInterface(service)”,那么您可以将服务强制转换为本地服务。
本地服务示例在服务中执行此操作:
在 ServiceConnection 类的 Activity 中执行此操作:
He is probably using Android Interface Definition Language (AIDL)
http://developer.android.com/guide/components/aidl.html
Therefore he has to use a stub of the server side implementation like documented:
The iservice reference is coming from the onServiceConnected method which is called after binding the service to your activity. The call bindService gets passed the ServiceConnection which implements the onServiceConnected method.
You don't need the "IRemoteService.Stub.asInterface(service)" when your implementation of the service is local, then you can just cast the service to you local service.
The local service sample does this in the service:
And this in the Activity in the ServiceConnection class:
在这里,我的例子..会让你清楚,哈哈
Here you are, my example .. will make you clear about that LOL