服务绑定返回true,但Service对象为null

时间:2014-03-25 12:38:36

标签: android service

在什么情况下调用bindService会返回true,但onServiceConnected从不运行,从而使我的服务对象为空?

代码

// Xmpp Connection Service Binding
private BackgroundXmppConnector mService;
private boolean mBound = false;
private XmppBinder binder;  

// SERVICE CONNECTION //////////////////////////////////////////////////////////////////////////
private ServiceConnection mConnection = new ServiceConnection()
{
    @Override
    public void onServiceConnected(ComponentName className, IBinder service)
    {
        Log.i("Main", "Service is connected");
        binder = (XmppBinder)service;
        mService = binder.getService();
        mBound = true;
    }

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

以及我如何与服务沟通

            // bind to the xmpp service
            Intent iXmpp = new Intent(getApplicationContext(), BackgroundXmppConnector.class);
            if(bindService(iXmpp, mConnection, Context.BIND_AUTO_CREATE))
            {
                Log.i("Main", "Status of bind: " + mBound + " and service connection: " + mService.toString());
                // Request from Xmpp
                iXmpp.putExtra("MESSAGEDATA", new Gson().toJson(
                    Utility.makeTransaction(getApplicationContext(), MessageType.Type.POPULATE, pop)
                    ));             
                mService.sendMessage(iXmpp);

                // unbind from our service
                unbindService(mConnection);
            }

,当我在绑定

之后检查service对象的状态时发生NPE
03-25 12:36:33.349: E/AndroidRuntime(19638): FATAL EXCEPTION: main
03-25 12:36:33.349: E/AndroidRuntime(19638): java.lang.NullPointerException
03-25 12:36:33.349: E/AndroidRuntime(19638):    at com.goosesys.gaggle.Main.onKeyMultiple(Main.java:439)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.view.KeyEvent.dispatch(KeyEvent.java:2644)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.app.Activity.dispatchKeyEvent(Activity.java:2361)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1887)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3577)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3533)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3115)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4157)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4136)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2932)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.os.Looper.loop(Looper.java:137)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at android.app.ActivityThread.main(ActivityThread.java:4810)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at java.lang.reflect.Method.invoke(Method.java:511)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-25 12:36:33.349: E/AndroidRuntime(19638):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

当您的主UI线程调用ServiceConnection时,无法保证调用bindService()回调。连接回调相对于此是异步的,因此在mService返回true后,您不会立即获得bindService()的值。 true值只表示绑定已发生,但在连接回调被命中之前,您将无法建立完整连接。

答案 1 :(得分:0)

我现在通过使用以下代码设法解决了这个问题:

        // bind to the xmpp service
        Intent iXmpp = new Intent(getApplicationContext(), BackgroundXmppConnector.class);
        bindService(iXmpp, mConnection, Context.BIND_AUTO_CREATE);

        Log.i("Main", "Status of bind: " + mBound + " and service connection: " + mService.toString());
        // Request from Xmpp
        iXmpp.putExtra("MESSAGEDATA", new Gson().toJson(
        Utility.makeTransaction(getApplicationContext(), MessageType.Type.POPULATE, pop)
                ));             
        mService.sendMessage(iXmpp);

        // unbind from our service
        unbindService(mConnection);

我真的不明白为什么会这样,但确实如此。我现在很高兴:)