开发者

Accessing Android Service Through Global Object

I've a simple service that several of my activiti开发者_运维问答es need to access. The standard approach seems to be:

  1. Create ServiceConnection to specify what to do after binding [with the service object]
  2. call bindService with ServiceConnection

But that requires calling bindService at least once per activity. What if I call bindService exactly once, store the service object globally to allow for different activities to access it?

Is this bad practice? Why?


You could extend Application and let it keep your reference to the service, then in your activity you cast getApplicationContext() to your new class and use a public API to get the service.

mService = ((MyApplication)getApplicationContext()).getMyService();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜