如何从活动调用服务的方法? [英] How to call methods of a Service from activity?

查看:23
本文介绍了如何从活动调用服务的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从我的活动中调用本地服务的方法.我该怎么做?

I simply want to call methods of a local service from my activity. How can I do that ?

推荐答案

一种方法是定义一个带有 Android 的 AIDL 并利用 Binder 子系统执行 IPC.我发布的链接中有一组很棒的说明.如果你有问题,我会从那里开始,然后在这里发帖.尽管是一个非常复杂的主题 (IPC) Android 和 Binder 做得非常好,使它非常简单(至少在开始时,我相信如果你愿意,你可以让它变得复杂;-) )

One way to do this is by defining an interface with Android's AIDL and making use of the Binder subsystem to perform IPC. There is a great set of instructions at the link I posted. I'd start there and then post here if you have questions. Despite being a pretty complex topic (IPC) Android and the Binder do a really good job of making it pretty dead simple (at least to get started, I'm sure you could make it complicated if you wanted to ;-) )

编辑 正如评论中所指出的,如果 Service 和客户端在同一进程中运行,则不需要这样做.除非您另行指定,否则这是默认设置.但是,无论如何它仍然有效,只是增加了一点复杂性.

Edit As pointed out in the comments, this is unnecessary if the Service and the client are running in the same process. Unless you specify otherwise, this is the default. However, it still works regardless, it just adds a bit more complexity.

这篇关于如何从活动调用服务的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆