2014-03-25 99 views
0

在什麼情況下調用bindService返回true,但onServiceConnected從不運行,從而使我的服務對象爲空?服務綁定返回true,但服務對象爲空

代碼

// 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); 
      } 

和NPE發生的歷史交流的時候我檢查service對象的狀態,綁定後

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) 
+0

Main.java的相關部分已包含在內。其他一切都與我的問題完全無關。 – LokiSinclair

回答

2

The ServiceConnection當您的主UI線程調用bindService()時,不保證回調被調用。相對於此,連接回調是異步的,因此在bindService()返回true後,您的mService將不會有值。 true值只是表示綁定發生了,但在連接回調被觸發之前,您還沒有建立完整的連接。

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); 

我真的不明白爲什麼這個工作,但它沒有。我現在很高興:)