2012-06-11 25 views
7

* EDIT RISOLTO in questo modo: * ho risolto cambiando il mio onDestroy() della mia attività e la ServiceConnection::onServiceDisconnected().Incapace di distruggere attività (il servizio non è stato registrato)

Ho aggiunto un booleano per verificare se sono limitato al servizio chiamato boundToService. Il ServiceConnection:

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     myService = ((EventService.MyBinder) service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName name) { 
     // TODO Auto-generated method stub 
     Log.i(getPackageName(), "ServiceConnection::onServiceDisconnected() called"); 
     boundToService = false; 
    } 
}; 

E il onDestroy():

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Log.e(getPackageName(), "Destroying Activity"); 
    if (boundToService && mConnection != null) { 
     doUnbindService(); 
    } 
} 

In questo modo il legame e non vincolante è fatto nell'attività:

public void doBindService() { 
    bindService(new Intent(this, EventService.class), mConnection, 
      Context.BIND_AUTO_CREATE); 
    boundToService = true; 
} 

public void doUnbindService() { 
    unbindService(mConnection); 
    boundToService = false; 
} 

Come detto Guillaume, chiamando super.onDestroy() primo è un dovere assoluto.

* FINE DI RISOLVERE *

Ho un Foreground Service che i dati GPS sondaggi e le MapActivity richieste che i dati ogni 3 secondi. Quando il onDestroy() del MapActivity si chiama, ottengo questo errore LogCat:

06-11 21:26:35.591: D/CLIPBOARD(14801): Hide Clipboard dialog at Starting input: finished by someone else... ! 
06-11 21:26:44.451: D/dalvikvm(14801): Debugger has detached; object registry had 1371 entries 
06-11 21:26:44.451: D/AndroidRuntime(14801): Shutting down VM 
06-11 21:26:44.456: W/dalvikvm(14801): threadid=1: thread exiting with uncaught exception (group=0x40c4b1f8) 
06-11 21:26:44.476: E/AndroidRuntime(14801): FATAL EXCEPTION: main 
06-11 21:26:44.476: E/AndroidRuntime(14801): java.lang.RuntimeException: Unable to destroy activity {com.project4.mtl/com.project4.mtl.EventActivity}: java.lang.IllegalArgumentException: Service not registered: [email protected] 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3124) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3142) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.ActivityThread.access$1200(ActivityThread.java:127) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1192) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.os.Looper.loop(Looper.java:137) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.ActivityThread.main(ActivityThread.java:4507) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at java.lang.reflect.Method.invokeNative(Native Method) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at java.lang.reflect.Method.invoke(Method.java:511) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at dalvik.system.NativeStart.main(Native Method) 
06-11 21:26:44.476: E/AndroidRuntime(14801): Caused by: java.lang.IllegalArgumentException: Service not registered: [email protected] 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:888) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.ContextImpl.unbindService(ContextImpl.java:1211) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.content.ContextWrapper.unbindService(ContextWrapper.java:375) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at com.project4.mtl.EventActivity.onDestroy(EventActivity.java:175) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.Activity.performDestroy(Activity.java:4629) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1082) 
06-11 21:26:44.476: E/AndroidRuntime(14801): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3111) 
06-11 21:26:44.476: E/AndroidRuntime(14801): ... 11 more` 

E naturalmente il codice intorno alla linea 175, in cui l'eccezione è causata nel mio programma:

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    unbindService(mConnection); 
    super.onDestroy(); 
} 

Se si vuole di più frammenti di codice, dimmelo.

* modificare *

Questo è il codice per la ServiceConnection:

public void doBindService() { 
    //ComponentName service = startService(new Intent(this, 
     // EventService.class)); 
    bindService(new Intent(this, EventService.class), mConnection, 
      Context.BIND_AUTO_CREATE); 
} 

mConnection è tipo di ServiceConnection:

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     myService = ((EventService.MyBinder) service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName name) { 
     // TODO Auto-generated method stub 

    } 
}; 
+0

È possibile che il servizio venga distrutto prima che l'attività venga distrutta? – SirPentor

+0

Il servizio viene distrutto per primo. – tolgap

+0

Questo è il problema principale allora. Come viene distrutto? – SirPentor

risposta

5

che ha funzionato per me! Basta chiamare la funzione unbindService() da getApplicationContext() invece di "questo" contesto. (Lo stesso per il legame del servizio)

getApplicationContext().unbindService(mServiceConnection); 
0

Si può solo impostare mService per nulla in onServiceDisconnected e controllare che nel onDestroy. Come questo:

private ServiceConnection mConnection = new ServiceConnection() { 
     @Override 
     public void onServiceConnected(ComponentName className, IBinder binder) { 
      mService = ((LoadArticlesService.LoadArticlesBinder) binder).getService(); 
     } 
     public void onServiceDisconnected(ComponentName className) { 
      mService = null; 
     } 
    }; 

OnDestroy:

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if (mService != null) { 
     unbindService(mConnection); 
    } 
} 

funziona bene.

Problemi correlati