Android的我怎么等到服务实际上连接? [英] Android how do I wait until a service is actually connected?

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

问题描述

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

 公共类下载扩展活动{
 IDownloaderService下载= NULL;
// ...

在Downloader.onCreate(包)我试图bindService

 意图serviceIntent =新意图(这一点,DownloaderService.class);
如果(bindService(serviceIntent,SC,BIND_AUTO_CREATE)){
  // ...

和ServiceConnection对象SC我这样做是在

 公共无效onServiceConnected(组件名名称,服务的IBinder){
  Log.w(XXX,onServiceConnected);
  下载= IDownloaderService.Stub.asInterface(服务);
  // ...

通过添加各种Log.xx我发现,code。如果(bindService(...))实际上走之前ServiceConnection.onServiceConnected被调用后 - 也就是,当下载仍然是空 - 这得到我陷入麻烦。在ApiDemos所有样品只有当用户操作触发的呼叫服务避免这一计时问题。但我应该做的到右使用这项服务bindService成功后?我如何可以等待ServiceConnection.onServiceConnected可靠地叫什么?

另一个问题有关。是否所有的事件处理程序:Activity.onCreate,任何View.onClickListener.onClick,ServiceConnection.onServiceConnected等实际调用在同一个线程(在doc中提到的主线)?它们之间有交错,或Android会安排所有事件进入正在处理一个接一个?或者,当究竟是ServiceConnection.onServiceConnected实际上是要叫?一旦Activity.onCreate完成或某个时候A.oC仍在运行?


解决方案

  

我怎么能等待
  ServiceConnection.onServiceConnected
  可靠地叫?


您没有。你退出的onCreate出()(或任何你要绑定),你把你需要建立连接,在 onServiceConnected(code )


  

是否所有的事件处理程序:
  Activity.onCreate,任何
  View.onClickListener.onClick,
  ServiceConnection.onServiceConnected,
  等实际调用在同一
  螺纹



  

在到底是
  ServiceConnection.onServiceConnected
  实际上将要叫什么?上
  Activity.onCreate完成或
  有时当A.oC仍在运行?


您绑定请求可能甚至不打算的启动的,直到你离开后,的onCreate()。因此, onServiceConnected()会叫你离开后的某个时间的onCreate()

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

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

In Downloader.onCreate(Bundle) I tried to bindService

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

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);
  // ...

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?

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?

解决方案

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

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

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

Yes.

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

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天全站免登陆