0

我有以下代码:

public void refreshWidget() {
    doBindService();
     mBoundService.loadState();
    doUnbindService();
}


private static ServiceConnection mAppWidgetServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service. Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.
        mBoundService = ((AppWidgetService.LocalBinder) service)
                .getService();
    }

    @Override
    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        // Because it is running in our same process, we should never
        // see this happen.
        mBoundService = null;
    }
};

static void doBindService() {
    // Establish a connection with the service. We use an explicit
    // class name because we want a specific service implementation that
    // we know will be running in our own process (and thus won't be
    // supporting component replacement by other applications).
    if (!mIsBound) {
        // mContext.startService(new Intent(mContext,
        // AppWidgetService.class));
        mContext.bindService(new Intent(mContext,
                AppWidgetService.class),
                mAppWidgetServiceConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }
}

怎么会mBoundService==null 在这一行mBoundService.loadState();

我错过了什么?

4

1 回答 1

0

你不能调用mBoundService.loadState(); doBindService() 之后;立即,因为此时服务不受限制。

调用mBoundService.loadState(); 在回调onServiceConnected之后。

于 2013-08-25T12:12:21.683 回答