• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android 等到服务实际连接?

用户头像
it1352
帮助1

问题说明

我有一个活动调用在 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?

正确答案

#1

我怎么等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().

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhcic
系列文章
更多 icon
同类精品
更多 icon
继续加载