-1

每个示例都有服务绑定 onStart() 并在一段时间内从服务调用方法(如 onClick 之后)。直到确定绑定了 onClick 服务。

我遇到的问题是服务需要一段时间才能被绑定并且它是异步绑定的。因此,如果我启动需要立即从 Service 获取一些数据的 Activity - 它会崩溃,因为它尚未绑定。所以我需要让我的活动等待它的连接完成。我怎么做?

4

2 回答 2

0

您可以利用 pendingList 变量来处理这种情况:

private ICoreService mService;
private boolean mConnected = false;
private boolean mConnecting = false;
private List<Something> mPendingTasks = new ArrayList<Something>;

private void bindService() {     
        try {
            Intent service = new Intent();
            service.setClassName(Constants.THE_SERVICE_PACKAGE_NAME, Constants.THE_SERVICE_FULL_NAME);
            mContext.bindService(service, mServiceConnection, Context.BIND_AUTO_CREATE);
            mConnecting = true;
        } catch (Exception e) {
              //handle exception
        }        
}

public void serviceDoSomething(Something task) {
    try {
        if (mService == null) {
            synchronized (mPendingTasks) {
                mPendingTasks.add(task);
            }
        } else {
            mService.doSomething(task);
        }
    } catch (Exception e) {
       //handle exception
    }
}

private ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mConnected = false;
        mConnecting = false;
        mService = null;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mConnected = true;
        mConnecting = false;
        mService = ICoreService.Stub.asInterface(service);
        drainPendingTasks();
    }
};

private void drainPendingTasks() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (mPendingTasks) {
                try {
                    if (!mPendingTasks.isEmpty()) {
                        for (Something task : mPendingTasks) {
                            mService.doSomething(task);
                        }
                        mPendingTasks.clear();
                    }
                } catch (Exception e) {
                    //handle exception
                }
            }
        }
    }).start();
}
于 2016-11-28T07:01:33.980 回答
-1

我想你想要那个:http: //developer.android.com/guide/components/bound-services.html

在此页面上,您可以找到:

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

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

当服务准备好时调用该onServiceConnected方法,你可以在里面做你的事情(或使用 mBound 属性)。

要绑定到您的服务,请使用:

    // Bind to LocalService
    Intent intent = new Intent(this, LocalService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
于 2013-07-23T08:49:46.320 回答