首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何延迟onServiceConnected呼叫

如何延迟onServiceConnected呼叫
EN

Stack Overflow用户
提问于 2013-08-17 10:47:19
回答 1查看 717关注 0票数 1

我正在实现Service,它与服务器建立TCP连接,然后允许客户端通过此连接传递消息。客户端通过bindService调用连接到服务。因此,onServiceConnected调用了客户端ServiceConnection对象。问题是,onServiceConnected是在从bindService返回后立即调用的,但是目前还没有建立与服务器的连接。当连接未建立时,我能以某种方式延迟onServiceConnected呼叫吗?如果不可能,请为我的情况提出一些好的模式。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-18 09:50:13

你应该这样做:

服务代码:

代码语言:javascript
复制
class MyService implements Service {
    private boolean mIsConnectionEstablished = false;

    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        public MyService getService() {
            // Return this instance of LocalService so clients can call public
            // methods
            return MyService.this;
        }
    }

    public interface OnConnectionEstablishedListener {
        public void onConnectionEstablished();
    }

    private OnConnectionEstablishedListener mListener;

    @Override
    public void onCreate() {
       super.onCreate();

       new Thread( new Runnable() {
           @Override
           void run() {
               //Connect to the server here

               notifyConnectionEstablished();
           }
       }).start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private void notifyConnectionEstablished() {
        mIsConnectionEstablished = true;

        if(mListener != null) {
            mListener.onConnectionEstablished();
        }
    }


    public void setOnConnectionEstablishedListener(
        OnConnectionEstablishedListener listener) {

        mListener = listener

        // Already connected to server. Notify immediately.
        if(mIsConnectionEstablished) {
            mListener.onConnectionEstablished();
        }
    }
}

活动代码:

代码语言:javascript
复制
class MyActivity extends Activity implements ServiceConnection,
    OnConnectionEstablishedListener {

    private MyService mService;
    private boolean mBound;

    @Override
    public void onCreate() {
       super.onCreate();

       //bind the service here
       Intent intent = new Intent(this, MyService.class);
       bindService(intent, this, BIND_AUTO_CREATE);
    }

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;

        mService.setOnConnectionEstablishedListener(this);
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }

    @Override
    public void onConnectionEstablished() {
        // At this point the service has been bound and connected to the server
        // Do stuff here
        // Note: This method is called from a non-UI thread.
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18287918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档