简体   繁体   中英

Binding Multiple Activities(Tabs) to a Service using a Base Class Activity

Found these after posting:

http://code.google.com/p/android/issues/detail?id=2665 http://code.google.com/p/android/issues/detail?id=2483

MyApp - first Activity that calls startService();
AwesomeService - background Service that starts a "connection" thread
ConnectedActivity - Activity with code to handle connection and service binding
MyAppActivity - Tabbed Activity
TabAActivity - Tab Activity

I have an Activity base class called ConnectedActivity. In this class I handle binding to a background Service that is connected to a remote system. The ConnectedActivity also has observer methods that capture changes in the connection and other notifications from the Service.

For my other Activities, that need to be "connected", I simply extend the ConnectedActivity.

I have developed a Tabbed Activity, for which the Tabs are also Activities. For the Tabbed Activity and each of the Tabs, I need to extend the ConnectedActivity.

The problem I am having is that when I load the Tabbed Activity (and Tab Activities), it is the only Activity that binds to the Service. The Tab Activities don't bind to the Service.

Below, serviceBound() is a callback that I have implemented in the ConnectedActivity class so that classes that are extending the ConntectedActivity can take action once the service is bound. You can see from my Log file that only the MyAppActivity is actually binding.

Is it possible for multiple Activities to bind to a Service?

found this here 这里找到

Multiple clients can connect to the service at once. However, the system calls your >service's onBind() method to retrieve the IBinder only when the first client binds. The system then delivers the same IBinder to any additional clients that bind, without calling onBind() again

So now my real question becomes.. does this mean the callbacks (onServiceConnected() & onServiceDisconnected) do not get called in the "second" activity that's binding?

12-31 02:40:00.625: I/MyApp(31345): onCreate(Bundle savedInstanceState)
12-31 02:40:00.705: I/AwesomeService(31345): onCreate()
12-31 02:40:00.705: I/AwesomeService(31345): onStartCommand(Intent intent, int flags, int startId)
12-31 02:40:00.725: I/ConnectedActivity(31345): onCreate(Bundle savedInstanceState)
12-31 02:40:00.725: I/MyAppActivity(31345): onCreate(Bundle savedInstanceState)
12-31 02:40:00.836: I/ConnectedActivity(31345): onCreate(Bundle savedInstanceState)
12-31 02:40:00.836: I/TabAActivity(31345): onCreate(Bundle savedInstanceState)
12-31 02:40:00.896: I/ConnectedActivity(31345): onStart()
12-31 02:40:00.896: I/TabAActivity(31345): onStart()
12-31 02:40:00.956: I/ConnectedActivity(31345): onStart()
12-31 02:40:00.966: I/MyAppActivity(31345): onStart()
12-31 02:40:00.966: I/ConnectedActivity(31345): onResume()
12-31 02:40:00.966: I/MyAppActivity(31345): onResume()
12-31 02:40:00.966: I/ConnectedActivity(31345): onResume()
12-31 02:40:00.966: I/TabAActivity(31345): onResume()
12-31 02:40:00.976: I/AwesomeService(31345): onBind(Intent intent)
12-31 02:40:01.056: I/ConnectedActivity(31345): onServiceConnected(ComponentName className, IBinder service)
12-31 02:40:01.056: I/AwesomeService(31345): LocalBinder:getService()
12-31 02:40:01.056: I/MyAppActivity(31345): serviceBound()

I was hoping to also see:

AwesomeService(31345): onBind(Intent intent)
ConnectedActivity(31345): onServiceConnected(ComponentName className, IBinder service)
AwesomeService(31345): LocalBinder:getService()
TabAActivity(31345): serviceBound()

Ok, so maybe I was spoiled by using stackoverflow.com before and getting quick responses. Maybe it being so close to the New Year and everyone besides me has a life, I was forced to dig deeper! I edited the above post to include these links:

http://code.google.com/p/android/issues/detail?id=2665 http://code.google.com/p/android/issues/detail?id=2483

The most interesting information found at one of those links, is the actual solution:

Comment 2 by olivier....@gmail.com, Jun 28, 2009 Using getApplicationContext().bindService instead of just bindService on your activity solves the problem as it is using the higher level application context.

To add to Olivier's comment, make sure and use the same Context for the unBindService() call as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM