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

标签 android background-service

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

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/

相关文章:

android - 如何从后台服务向 Activity 发送数据?

android - 通过返回按钮关闭应用程序时后台服务停止

android - 控制接近传感器的后台服务

java - Admob 原生快捷广告

android - LocationManager.requestLocationUpdates 添加或更新?

android - Xamarin 形成 RelativeLayout 使用

android - 工作管理器是否适合在后台播放音乐?

android - 棉花糖安卓中永无止境的后台服务

javascript - Android - 如何在运行时生成的 HTML 数据中加载外部 javascript 文件?

android - (ActionBar) Tabs + Pager + detail Fragments inside ViewPager 容器