6

Androidで外部サービス(別のjarにある)ファイルをバインド解除する適切な方法は何ですか?

org.eclipse.paho.android.service-1.0.2.jarマニフェスト ファイルでサービス ( にある) を次のように宣言します。

<service android:name="org.eclipse.paho.android.service.MqttService" >
</service>

サービスソースはこちら

そして、サービスは適切に実行されます。ただし、アプリを終了するとエラーが発生し、検索を行った後、アクティビティのメソッドServiceConnectionLeakedを呼び出す必要があることがわかりました。unbindService(...);onDestroy()

ログ:

24915-24915/com.testapp.testmqtt E/ActivityThread﹕ Activity com.testapp.testmqtt.MainActivity has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@426c50d8 that was originally bound here
    android.app.ServiceConnectionLeaked: Activity com.testapp.testmqtt.MainActivity has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@426c50d8 that was originally bound here

ただし、サービスを手動で開始するわけではないため、ServiceConnection渡す必要はありません。unbindService()そのサービスを取得してServiceConnectionバインドを解除する方法はありますか?

4

1 に答える 1

14

このコードをアクティビティの onDestroy に入れるだけです

    client.unregisterResources();
    client.close();
于 2015-06-12T15:46:58.570 に答える