简体   繁体   中英

Accessing one service from two activities

I have an app for which I have created a background service to provide GPS location updates - because GPS will not normally give fast updates when the activity is in the background for battery consumption reasons.

It basically works fine, service is started from the main activity then bound to, and I can then access it and get location callbacks with no problem.

But, I need to access this same service from a different activity in the same app. So in the second activity I have only bound to the service (not started it - it is already going).

The service seems to provide location updates to the second activity OK, but my question is - is it the same instance of the service, or has a second instance been started? The reason for the question is the LogCat output (below):

11-06 12:49:45.939 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MA: in onCreate()
11-06 12:49:45.940 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MA: starting service
11-06 12:49:45.942 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MA: binding service
11-06 12:49:45.949 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - BC: in constructor
11-06 12:49:45.973 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - BC: in onStart()
11-06 12:49:46.011 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService: in constructor
    in onCreate()
11-06 12:49:46.025 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService: in onBind()
11-06 12:49:46.111 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MA: service connected
11-06 12:50:27.048 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
.... (lots more locations here - below is where it swapped to activity #2)
11-06 12:51:05.044 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:05.650 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MAP: in onCreate()
    binding service
11-06 12:51:06.773 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MAP: service connected
11-06 12:51:06.776 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:07.040 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:07.411 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - BC: in onStop()
11-06 12:51:08.046 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
.... (here is where I put the 2nd activity into the background)
11-06 12:51:22.058 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:22.379 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MAP: in onDestroy() (unbinding service)
11-06 12:51:22.383 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MAP: in onDestroy (stopping service)
11-06 12:51:23.038 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:24.040 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()

The confusion is that when I put the second activity into the background, you can see that it went through the activity onDestroy(), unbinding and then stopping the service - but after that there were still location updates coming through.

Is this because (a) there are actually two separate instances of the service (I hope not), or (b) because it didn't stop the service because the original activity was still connected to it, or (c) something else?

Found the answer myself - there is just one service instance, accessed from both activities.

Did this by storing the activity name in the service and reading and writing it from each activity.

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