从 Activity 的 onCreate 检测服务可用性
从 Activity
内的 onCreate
中,我希望能够检测不同应用程序中的 Service
是否(我正在还写)可用并正在运行。 AIDL 的问题是没有同步选项。我希望我的活动能够真正等待,看看 Service
是否可用,如果不可用,则采取相应的行动。
我想出的最佳答案是让服务打开 TCP 套接字并让 Activity 尝试连接到它。如果成功,则 Activity 的 onCreate
将知道该服务可用。然而,这充其量只是一个拼凑。
因此,底线是:我的 Activity 的 onCreate 同步检查外部 Service 是否可用,并在从 onCreate 例程返回之前采取操作?
From within the onCreate
within an Activity
, I'd like to be able to detect whether a Service
in a different application (that I'm also writing) is available and running. The problem with AIDL is that there is no synchronous option. I'd like my activity to actually wait to see if the Service
is available and act accordingly if it is not.
The best answer I've come up with is for the service to open a TCP socket and for the Activity to try to connect to it. If that succeeds, then the Activity's onCreate
will know that the service is available. However this is a kludge at best.
So, bottom line: how can my Activity's onCreate
synchronously check to see if an external Service
is available and take action before returning from the onCreate
routine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以循环访问 public
List返回的列表getRunningServices(int maxNum)
。每个ActivityManager.RunningServiceInfo
包含您需要的信息检查 API 此处和此处
You can loop through the List returned by public
List<ActivityManager.RunningServiceInfo> getRunningServices (int maxNum)
. EachActivityManager.RunningServiceInfo
contains the information you needCheck the API here and here