1

私はリモート サービスを持っています。このサービスを使用して の状態を確認したいと考えていますonPrepareOptionsMenu。しかし、他のアクティビティから戻ったときに、「binder null」例外が発生することがありました。でバインダーをnullに設定しましたonServiceDisconnected

私の質問:

  1. onServiceDisconnectedアクティビティ onStop の後に呼び出されますか?
  2. bindServiceonStart代わりに入れたほうがいいonCreateですか?
  3. ローカルサービスとリモートサービスの違いを簡単に説明していただけますか?
4

1 に答える 1

0

1) No. onServiceDisconnected() only gets called when the Service terminates unexpectedly, like when the process hosting the Service crashes. It will not be called when you unbind from the Service yourself. From the documentation:

Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does not remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to onServiceConnected(ComponentName, IBinder) when the Service is next running.

2) Generally, if you don't need to be connected to the Service while your Activity is stopped, then yes, binding in onStart() and unbinding in onStop() is the right way to go. The idea here is that you won't be wasting resources when your Activity is in the background. However, the documentation for bound services state that binding in onCreate() and unbinding in onDestroy() is OK too in some cases.

3) A local Service implies that the Service is in the same process as the consumer. A remote service is a Service that is in a different process.

于 2012-11-20T15:55:10.770 に答える