Android L 中 getRunningTasks 的替代方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26400469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:21:21  来源:igfitidea点击:

Alternative to getRunningTasks in Android L

androidandroid-5.0-lollipop

提问by MRK

It's been well documented that getRunningTasks is now deprecated in Android L. I used this to get top activity and this was an important feature of my app. Though not complete, a new solution in Android L has been promised by a Google Employee. The solution states:

有据可查的是,现在 Android L 中不推荐使用 getRunningTasks。我用它来获取顶级活动,这是我的应用程序的一个重要功能。尽管尚未完成,但 Google 员工已承诺在 Android L 中提供新的解决方案。解决方案指出:

"for L we do plan to have a new solution that will address some of the existing use cases of getRecentTasks(): we hope to expose the system's internal usage stats service in a new and expanded form that is available to third party apps through the SDK.

“对于 L,我们确实计划有一个新的解决方案来解决 getRecentTasks() 的一些现有用例:我们希望以一种新的扩展形式公开系统的内部使用统计服务,该服务可通过第三方应用程序使用开发工具包。

The new usage stats service should have two major pieces of information. First, it will provided aggregated stats of the time the user spent in each app, and the last launch time of those apps, rolled up over days, weeks, months, and years. Second, it will include detailed usage events over the last few days, describing the time and ComponentName when any activity transitions to the foreground or background.

新的使用统计服务应该有两个主要信息。首先,它将提供用户在每个应用程序中花费的时间以及这些应用程序的上次启动时间的汇总统计数据,这些统计数据汇总了数天、数周、数月和数年。其次,它将包括过去几天的详细使用事件,描述任何活动转换到前台或后台时的时间和 ComponentName。

Access to this new usage data will require that the user explicitly grant the app access to usage stats through a new settings UI. They will be able to see all apps that have access and revoke access at any point.

访问这些新的使用数据需要用户通过新的设置 UI 明确授予应用程序访问使用统计信息的权限。他们将能够随时查看所有具有访问权限和撤销访问权限的应用程序。

I realize that this won't cover every possible use of the old getRecentsTask API, and I apologize for that. However, for what we see to be the key current uses of the API, a new usage stats API should be much more robust and efficient."

我意识到这不会涵盖旧 getRecentsTask API 的所有可能用法,我为此道歉。但是,对于我们所看到的 API 当前的主要用途,新的使用统计 API 应该更加强大和高效。”

You can read more about it here: https://code.google.com/p/android-developer-preview/issues/detail?id=29#c50

您可以在此处阅读更多相关信息:https: //code.google.com/p/android-developer-preview/issues/detail?id=29#c50

I have tried looking in the Android L documentation but cannot find anything that talks the usage stats service mentioned above. Has anyone used this API? Can anyone point me to the documentation for this?

我曾尝试查看 Android L 文档,但找不到任何与上述使用情况统计服务相关的内容。有人用过这个API吗?任何人都可以指出我的文档吗?

采纳答案by ben75

The system statistics api evoked in your question is also presented hereand the detailed API is here. As far as I can see this API won't allow you to get the current top activity.

在您的问题中调用的系统统计 api 也在这里提供,详细的 API 在这里。据我所知,此 API 不允许您获取当前的顶级活动。

i.e. you have the ability to receive UsageEventwith the type MOVE_TO_FOREGROUNDby using UsageStatsManager.queryEventsbut according javadoc:

即您有能力通过使用但根据javadoc接收UsageEvent类型:MOVE_TO_FOREGROUNDUsageStatsManager.queryEvents

The last few minutes of the event log will be truncated to prevent abuse by applications.

事件日志的最后几分钟将被截断以防止应用程序滥用。

So it is really an API to get statistics, not to get realtime information. (note that the now deprecated getRunningTasks()was neither intended to retrieve realtime data : javadoc)

所以它实际上是一个获取统计信息的 API,而不是获取实时信息。(请注意,现在已弃用的getRunningTasks()既不打算检索实时数据:javadoc

On the other hand, if you are only interested in top activity of your own app: you can use getAppTasks()returning a List<AppTask>where you can get the RecentTaskInfowith getTaskInfo

另一方面,如果您只对自己的应用程序的顶级活动感兴趣:您可以使用getAppTasks()返回一个List<AppTask>可以获取RecentTaskInfo的位置getTaskInfo



EDIT : another approach is provided here.

编辑:这里提供另一种方法。

It is based on UsageStatsand UsageStats.getLastTimeUsed(). Drawback : it requires additional permission and gives you only the package name -not the activity-, but it gives you real time data.

它基于UsageStatsUsageStats.getLastTimeUsed()。缺点:它需要额外的权限,并且只给你包名——不是活动——,但它给你实时数据。