我有一个活动呼叫中IDownloaderService.aidl定义的服务:Android如何等待服务实际连接?
public class Downloader extends Activity {
IDownloaderService downloader = null;
// ...
在Downloader.onCreate(束)我试图bindService
Intent serviceIntent = new Intent(this, DownloaderService.class);
if (bindService(serviceIntent, sc, BIND_AUTO_CREATE)) {
// ...
和内ServiceConnection对象SC我这样做
public void onServiceConnected(ComponentName name, IBinder service) {
Log.w("XXX", "onServiceConnected");
downloader = IDownloaderService.Stub.asInterface(service);
// ...
通过添加各种Log.xx,我发现if(bindService(...))之后的代码实际上是BEFORE ServiceConnection.onSer viceConnected正在被调用 - 也就是说,当下载器仍然是空的 - 这让我陷入困境。 ApiDemos中的所有示例通过在用户操作触发时仅调用服务来避免此时间问题。但是我应该怎么做才能在bindService成功后正确使用这个服务?我如何等待ServiceConnection.onServiceConnected被可靠地调用?
另一个问题有关。是否所有的事件处理程序:Activity.onCreate,任何View.onClickListener.onClick,ServiceConnection.onServiceConnected等实际上是在同一个线程中调用的(在doc中称为“主线程”)?它们之间是否存在交错,或者Android会安排逐个处理所有事件?或者,什么时候ServiceConnection.onServiceConnected实际上会被调用?完成Activity.onCreate或A.oC仍在运行时?
如果你仍然需要一个答案,我给了一个解决方案在http://stackoverflow.com/a/22134635/1336747 – Alessio 2014-03-02 22:43:29
@Alessio,我不认为你的解决方案实际工作。 – Sam 2015-06-19 08:44:23