当正好onServiceConnected为有限服务将被调用? [英] When exactly onServiceConnected for bounded service will be called?

查看:150
本文介绍了当正好onServiceConnected为有限服务将被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这样的其他服务绑定服务:

I am trying to bind service from another service like this:

public class ServiceA extends Service {
    private ServiceB mDataService;
    private boolean mIsBound;

    @Override
    public void onCreate(){
        super.onCreate();
        doBindService();
        /* ... */
    }

    @Override
    public void onStart(final Intent intent, final int startId){
        /*...*/
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {            
            mDataService = ((ServiceB.LocalBinder)service).getService();
        }
        public void onServiceDisconnected(ComponentName className) {             
            mDataService = null;
        }
    };

    void doBindService() {          
        bindService(new Intent(ServiceA.this, ServiceB.class), mConnection, Context.BIND_AUTO_CREATE);          
        mIsBound = true;
    }

    void doUnbindService() {
        if (mIsBound) {                     
            unbindService(mConnection);
            mIsBound = false;
        }
    }       
}

这是一个简单的代码片段,我从Goolge的的样品了:)
在code工作得很好,并mDataService持有至ServiceB实例的引用,但有一件事我不明白: onServiceConnected 回调调用之后调用在onStart 。当我看到Android的文档,<一个href=\"http://developer.android.com/reference/android/content/ServiceConnection.html#onServiceConnected%28android.content.ComponentName,%20android.os.IBinder%29\"相对=nofollow>回调是在主线程运行 - 但我可以指望它,它总是会在我的情况下,这个顺序发生?的onCreate - >在onStart - > onServiceConnected

This is a simple snippet that I took from goolge's samples :) The code works just fine and mDataService holds a reference to ServiceB instance, but there is one thing I could not understand: the onServiceConnected callback is called after the call to onStart. As I saw on android's docs, the callback is running on the main thread - but can I count on it that it will ALWAYS happen in this order in my case? onCreate -> onStart -> onServiceConnected ?

推荐答案

它不是正式的开发者指南中已经明确指出,的Context.bindService()确实是一个异步调用,此也解释了为什么ServiceConnection.onServiceConnected()作为回调的实现。查看开发指南

It hasn't been clearly stated in the official dev guide, the Context.bindService() is indeed an asynchronous call, this also explain why ServiceConnection.onServiceConnected() is implemented as a callback. Check out the dev guide:

一个客户端可以通过调用bindService绑定到服务()。当它,它必须提供ServiceConnection的实施方案,其监视与该服务的连接。该bindService()方法立即返回没有价值,但是当Android系统会在客户端和服务之间的连接,它在ServiceConnection调用onServiceConnected(),为客户提供的IBinder客户端可以使用的服务进行通信。

A client can bind to the service by calling bindService(). When it does, it must provide an implementation of ServiceConnection, which monitors the connection with the service. The bindService() method returns immediately without a value, but when the Android system creates the connection between the client and service, it calls onServiceConnected() on the ServiceConnection, to deliver the IBinder that the client can use to communicate with the service.

ServiceConnection.onServiceConnected()是在未来的某个时刻呼吁UI线程(调用后不要立即Context.bindService()),一旦连接到服务是否正常建立。

ServiceConnection.onServiceConnected() is called on UI thread at some point in the future (not immediately after calling Context.bindService()), once connection to service is properly established.

这篇关于当正好onServiceConnected为有限服务将被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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