从两个活动访问一项服务
Accessing one service from two activities
我有一个应用程序,我为其创建了一个后台服务来提供 GPS 位置更新 - 因为当 activity 处于后台时出于电池消耗的原因,GPS 通常不会提供快速更新。
基本没问题,服务是从主activity开始,然后绑定到,然后我就可以访问它并毫无问题地获取位置回调。
但是,我需要从同一应用中的不同 activity 访问同一服务。所以在第二个 activity 中我只绑定了服务(没有启动它 - 它已经开始了)。
该服务似乎向第二个服务提供位置更新 activity 好的,但我的问题是 - 它是同一个服务实例,还是已经启动了第二个实例?问题的原因是 LogCat 输出(下):
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()
令人困惑的是,当我将第二个 activity 置于后台时,您可以看到它经历了 activity onDestroy(),解除绑定然后停止服务 - 但在那之后仍有位置更新。
这是因为 (a) 实际上有两个独立的服务实例(我希望不是),或者 (b) 因为它没有停止服务,因为原来的 activity 仍然连接到它,或者 (c) 其他东西?
我自己找到了答案 - 只有一个服务实例,可从两个活动访问。
通过将 activity 名称存储在服务中并从每个 activity.
读取和写入它来完成此操作
我有一个应用程序,我为其创建了一个后台服务来提供 GPS 位置更新 - 因为当 activity 处于后台时出于电池消耗的原因,GPS 通常不会提供快速更新。
基本没问题,服务是从主activity开始,然后绑定到,然后我就可以访问它并毫无问题地获取位置回调。
但是,我需要从同一应用中的不同 activity 访问同一服务。所以在第二个 activity 中我只绑定了服务(没有启动它 - 它已经开始了)。
该服务似乎向第二个服务提供位置更新 activity 好的,但我的问题是 - 它是同一个服务实例,还是已经启动了第二个实例?问题的原因是 LogCat 输出(下):
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()
令人困惑的是,当我将第二个 activity 置于后台时,您可以看到它经历了 activity onDestroy(),解除绑定然后停止服务 - 但在那之后仍有位置更新。
这是因为 (a) 实际上有两个独立的服务实例(我希望不是),或者 (b) 因为它没有停止服务,因为原来的 activity 仍然连接到它,或者 (c) 其他东西?
我自己找到了答案 - 只有一个服务实例,可从两个活动访问。
通过将 activity 名称存储在服务中并从每个 activity.
读取和写入它来完成此操作