Android 如何等到服务实际连接? [英] Android how do I wait until a service is actually connected?

查看:24
本文介绍了Android 如何等到服务实际连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动调用在 IDownloaderService.aidl 中定义的服务:

I have an Activity calling a Service defined in IDownloaderService.aidl:

public class Downloader extends Activity {
 IDownloaderService downloader = null;
// ...

在 Downloader.onCreate(Bundle) 中,我尝试绑定服务

In Downloader.onCreate(Bundle) I tried to bindService

Intent serviceIntent = new Intent(this, DownloaderService.class);
if (bindService(serviceIntent, sc, BIND_AUTO_CREATE)) {
  // ...

在 ServiceConnection 对象 sc 中我做了这个

and within the ServiceConnection object sc I did this

public void onServiceConnected(ComponentName name, IBinder service) {
  Log.w("XXX", "onServiceConnected");
  downloader = IDownloaderService.Stub.asInterface(service);
  // ...

通过添加各种 Log.xx,我发现 if(bindService(...)) 之后的代码实际上是在调用 ServiceConnection.onServiceConnected 之前 - 也就是说,当下载器仍然为空时 - 这让我陷入困境.ApiDemos 中的所有示例都通过仅在由用户操作触发时调用服务来避免此计时问题.但是在 bindService 成功后我应该怎么做才能正确使用这个服务?如何等待 ServiceConnection.onServiceConnected 被可靠地调用?

By adding all kinds of Log.xx I found that the code after if(bindService(...)) actually goes BEFORE ServiceConnection.onServiceConnected is being called - that is, when downloader is still null - which gets me into trouble. All the samples in ApiDemos avoid this timing problem by only calling services when triggered by user actions. But what should I do to right use this service after bindService succeeds? How can I wait for ServiceConnection.onServiceConnected being called reliably?

另一个相关的问题.是否所有事件处理程序:Activity.onCreate、任何 View.onClickListener.onClick、ServiceConnection.onServiceConnected 等实际上都在同一个线程中调用(在文档中称为主线程")?它们之间是否存在交错,或者Android会安排所有事件一一处理?或者,ServiceConnection.onServiceConnected 究竟何时被调用?Activity.onCreate 完成后还是 A.oC 仍在运行时?

Another question related. Are all the event handlers: Activity.onCreate, any View.onClickListener.onClick, ServiceConnection.onServiceConnected, etc. actually called in the same thread (mentioned in the doc as the "main thread")? Are there interleaves between them, or Android would schedule all events come into being handled one-by-one? Or, When exactly is ServiceConnection.onServiceConnected actually going to be called? Upon completion of Activity.onCreate or sometime when A.oC is still running?

推荐答案

我怎么等ServiceConnection.onServiceConnected被可靠地调用?

How can I wait for ServiceConnection.onServiceConnected being called reliably?

你没有.您退出 onCreate()(或您绑定的任何地方),并将需要建立连接"代码放入 onServiceConnected().

You don't. You exit out of onCreate() (or wherever you are binding) and you put you "needs the connection established" code in onServiceConnected().

都是事件处理程序:Activity.onCreate,任何View.onClickListener.onClick,ServiceConnection.onServiceConnected,等实际上调用相同线程

Are all the event handlers: Activity.onCreate, any View.onClickListener.onClick, ServiceConnection.onServiceConnected, etc. actually called in the same thread

是的.

具体是什么时候ServiceConnection.onServiceConnected居然会被调用?之上Activity.onCreate 的完成或什么时候 A.oC 还在运行?

When exactly is ServiceConnection.onServiceConnected actually going to be called? Upon completion of Activity.onCreate or sometime when A.oC is still running?

在您离开 onCreate() 之前,您的绑定请求可能甚至不会开始.因此,onServiceConnected() 会在您离开 onCreate() 后的某个时间调用.

Your bind request probably is not even going to start until after you leave onCreate(). Hence, onServiceConnected() will called sometime after you leave onCreate().

这篇关于Android 如何等到服务实际连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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