gpt4 book ai didi

android - 谷歌播放服务 : how to check if there is currently "active" pending intent callback registered to location updates/activity recognition?

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:00 25 4
gpt4 key购买 nike

我的应用程序在后台执行定期位置更新和 Activity 识别检测。

我正在使用 Google Play Services API 这样做:

例如 - 要注册到位置更新,我提供接收更新的未决 Intent :

mLocationClient.requestLocationUpdates(mLocationRequest, pendingInent);

要注销位置更新,我正在执行以下操作:

mLocationClient.removeLocationUpdates(pendingInent);

这很好,而且效果很好。

但是我如何才能知道当前是否有一个 pendingIntent 持有我的应用程序组件的 Intent 当前是否已在 Google Play 服务前注册以接收位置更新?

我注意到没有 API 可以检查这个,approach to check for pendingIntent existence似乎不适用于针对 Google Play 服务的 API - 即使在我调用 removeLocationUpdates() 方法后,未决 Intent 仍然存在。

我知道我可以保存状态(到共享首选项)以指示我现在是否已注册,但这不是正确的解决方案,并且会出现“错误”,因为 google play 进程可能会出现故障,丢失pendingIntent,但我的进程仍会认为位置更新是“Activity 的”。

Activity 识别更新完全存在同样的问题。

为了清楚起见,我想做的就是让我的应用程序的用户知道我的应用程序当前是否正在后台收集数据,并为他们提供一种在两者之间切换的方法。因此,如果有其他方法以可靠的方式做到这一点 - 我也会将其作为答案除外

可靠的方式 = 知道当前挂起的 Intent 是否真的在 Google Play 服务中注册...

  • 使用 LocationListener 对我来说不是一个选项,因为当我的进程停止时我必须能够接收更新事件 - 只有使用 PendingIntent 回调才有可能..

提前致谢。

更新

这是我提供的用于注册和注销位置更新的 pendintIntent:

Intent locationUpdatesIntent = new Intent(context, LocationUpdatesIntentService.class);
PendingIntent pendingInent = PendingIntent.getService(context, 0, locationUpdatesIntent, PendingIntent.FLAG_UPDATE_CURRENT);

这就是我试图检查(未成功)它是否已注册的方式:

Intent locationUpdatesIntent = new Intent(context, LocationUpdatesIntentService.class);
PendingIntent pendingInent = PendingIntent.getService(context, 0, locationUpdatesIntent, PendingIntent.FLAG_NO_CREATE);
boolean isLocationUpdatesEnabled = (pendingIntent != null);

isLocationUpdatesEnabled 在我调用 removeLocationUpdates()

后返回 true 事件

最佳答案

以下是删除 PendingIntent 时的情况:

  • PendingIntent 可以通过调用 PendingIntent.cancel 明确删除,只能由创建它的应用程序删除。来自docs :

Only the original application owning a PendingIntent can cancel it.

  • PendingIntent,如果处于非 Activity 状态,即未被其他应用程序使用,则在应用程序终止时被系统删除。
  • PendingIntent,如果处于 Activity 状态,即被其他应用程序使用,则只有在卸载应用程序包时才会被系统删除。

因此调用 removeLocationUpdates 表明系统 Google 位置服务不再引用 PendingIntent。它不会将其从系统中删除。所以你必须在 PendingIntent 上调用 cancel 来删除它,这绝对没问题。

mLocationClient.removeLocationUpdates(pendingInent);
pendingIntent.cancel();

//Now check whether PendingIntent exists.
Intent locationUpdatesIntent = new Intent(context, LocationUpdatesIntentService.class);
PendingIntent pendingInent = PendingIntent.getService(context, 0, locationUpdatesIntent, PendingIntent.FLAG_NO_CREATE);
boolean isLocationUpdatesEnabled = (pendingIntent != null);

来自 docs 的附加信息:

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.

关于android - 谷歌播放服务 : how to check if there is currently "active" pending intent callback registered to location updates/activity recognition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25176393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com