可以bindService()进行阻拦? [英] Can bindService() be made to block?

查看:256
本文介绍了可以bindService()进行阻拦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用远程服务的Andr​​oid应用程序,我绑定到它与 bindService(),这是异步的。

I have an Android application that uses a Remote Service and I bind to it with bindService(), which is asynchronous.

该应用程序是没有用的,直到服务绑定,所以我想干脆等到任何活动开始之前该绑定完成。有没有办法让前的onCreate绑定的服务() onResume()被称为?我觉得可能是没有办法做到的应用程序绑定。任何想法?

The app is useless until the service is bound, so I would like to simply wait until the binding is finished before any Activity is started. Is there a way to have the service bound before onCreate() or onResume() is called? I think there might be a way to do the binding in Application. Any ideas?

编辑:

如果在的onCreate()我做到这一点。

bindService(service, mWebServiceConnection, BIND_AUTO_CREATE);
synchronized (mLock) { mLock.wait(40000); }

在ServiceConnection.onServiceConnected不会被调用40秒。很明显,我必须让的onCreate()返回,如果我想的服务绑定。

The ServiceConnection.onServiceConnected doesn't get called for 40 seconds. It's clear that I have to let onCreate() return if I want the service to bind.

所以它似乎没有办法做我想要的。

So it appears there's no way to do what I want.

编辑2: <一href="http://stackoverflow.com/questions/3055599/android-how-do-i-wait-until-a-service-is-actually-connected">Android我怎么等到服务实际上连接?大约有约束力的服务时是怎么回事Android中一些很好的注释。

Edit 2: Android how do I wait until a service is actually connected? has some good commentary about what is going on in Android when binding a service.

推荐答案

您不能bindService()块。但是,您的ServiceConnection(第二个参数bindService)有回调告诉你的服务是连接或断开连接的时候,所以你可以有其他的code座,直到你的onServiceConnected()方法疏导它。

You cannot have bindService() block. However, your ServiceConnection (2nd parameter to bindService) has callbacks to tell you when the service is connected and disconnected, so you can have other code block until your onServiceConnected() method unblocks it.

这篇关于可以bindService()进行阻拦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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