我现在创建了一个在后台发送位置的服务
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/