gpt4 book ai didi

android - 后台 Activity 识别

转载 作者:行者123 更新时间:2023-12-04 23:54:37 26 4
gpt4 key购买 nike

我正在制作一个使用 ActivityRecognition API 在后台跟踪用户 Activity 的应用,如果用户在指定时间段(例如 1 小时)内停留在同一个地方,系统就会推送通知告诉用户去散步.我已经实现了 Activity 识别,但仅适用于打开应用程序的情况。显然,Google API 客户端需要保持连接才能发送 Activity 更新。我的问题是 - 对于后台 Activity 跟踪,什么是更好的解决方案:

1) 在主 Activity (或单独的 Activity )中实现 AlarmManager,每 30 秒唤醒一次 Activity ,将 Google API 客户端连接到 Play 服务,然后将 PendingIntent 发送到 IntentService 以进行 Activity 分析

2) 创建一个单独的服务(不是 IntentService)在后台(单独的线程)持续运行,这将保持 API 客户端连接,并将 Activity 更新发送到 IntentService。因此,系统将有 2 个服务:1) 保持 API 客户端连接到 Play Services 并将定期 Activity 更新发送到 IntentService 以供分析的服务; 2) IntentService 用于从服务接收 Activity 更新,并分析数据。

3) 其他一些解决方案(由你们提供)

点评:我的导师建议我使用AlarmManager,但你通常用它来做网络更新之类的事情,所以间隔一般在10分钟以上,而我需要30秒-1分钟。所以我很犹豫要不要用。

我以前也在这里看到过很多类似的问题,但是我没有找到明确的答案。

最佳答案

实际上,只有在请求和删除 Activity 更新时才需要连接的 GoogleApiClient - 您不需要连接的 GoogleApiClient 来接收 Activity 更新 .

requestActivityUpdates() documentation实际上特别提到在后台工作:

A common use case is that an application wants to monitor activities in the background and perform an action when a specific activity is detected. To do this without needing a service that is always on in the background consuming resources, detected activities are delivered via an intent. The application specifies a PendingIntent callback (typically an IntentService) which will be called with an intent when activities are detected. The intent recipient can extract the ActivityRecognitionResult using extractResult(android.content.Intent). See the documentation of PendingIntent for more details.

关于android - 后台 Activity 识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39552808/

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