从两项活动中访问一项服务

问题描述 投票:0回答:1

我有一个已为其创建后台服务的应用程序,以提供GPS位置更新-因为出于电池消耗原因,当活动处于后台时,GPS通常不会提供快速更新。

它基本上可以正常工作,服务是从主要活动启动的,然后绑定到它,然后我可以访问它并毫无问题地获取位置回调。

但是,我需要从同一应用程序中的不同活动访问该相同服务。因此,在第二个活动中,我仅绑定到该服务(未启动该服务-它已经在运行)。

该服务似乎可以为第二个活动提供位置更新,但是我的问题是-它是该服务的同一实例,还是已经启动了第二个实例?问题的原因是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()

[困惑是,当我将第二个活动放到后台时,您可以看到它通过活动onDestroy(),解除绑定,然后停止该服务-但之后仍然有位置更新通过。

之所以这样,是因为(a)实际上有两个单独的服务实例(我希望不是),或者(b)因为它仍然因为原始活动仍与该服务相连而没有停止该服务,或者(c)某个东西其他吗?

android android-service android-location android-service-binding
1个回答
0
投票

我自己找到了答案-只有一个服务实例,可以从这两个活动中访问。

通过将活动名称存储在服务中并从每个活动中读取和写入来进行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.