gpt4 book ai didi

android - 访问正在运行的服务并调用它的方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:55:10 27 4
gpt4 key购买 nike

我现在创建了一个在后台发送位置的服务

i want to create a method in my ACTIVITY which will access the currently running instance of service and will call its method

说定位服务有方法

SendLocation()
{
/// to do
}

现在在 Activity A 中:

Service s = getCurrentRunningServiceInstance // something like this

并调用它的方法

s.SendLocation();

最佳答案

您不能直接调用Service 上的方法。您需要做的是绑定(bind)服务

在您的 Service 中,覆盖 onBind() 并让它返回一个有效的 IBinder 然后以客户端/服务器方式使用它。

请参阅 Services 的文档特别是关于 Creating a Bound Service还有 Bound Services 的文档.

关于android - 访问正在运行的服务并调用它的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11305671/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com