如何使用UsageStatsManager? [英] How to use UsageStatsManager?

查看:1224
本文介绍了如何使用UsageStatsManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌已经去precated功能<一href="http://developer.android.com/reference/android/app/ActivityManager.html#getRecentTasks(int,%20int)">getRecentTasks" 的ActivityManager类。现在它所做的是让当前的应用程序已经打开的应用程序列表。

Google has deprecated the function "getRecentTasks" of "ActivityManager" class. Now all it does is to get the list of apps that the current app has opened.

我甚至写了一个关于它的帖子 在这里在计算器 ,但我发现这是不可能的。

I've even written a post about it here on StackOverflow, but I noticed it's impossible.

我做了一个关于它的帖子(<一href="https://$c$c.google.com/p/android-developer-$p$pview/issues/detail?id=507">here,而另一个类似的一个接别人,<创建的href="https://$c$c.google.com/p/android-developer-$p$pview/issues/detail?id=29">here)并要求重新考虑它,谷歌决定做一个新的类,似乎提供了类似的功能(更喜欢统计数据,也可能是有用的),但我不能找出如何使用它。

I've made a post about it (here, and another, similar one created by someone else, here) and requested to re-consider it, and Google decided to make a new class, that seem to provide a similar functionality (more like statistics, but might also be useful), but I can't find out how to use it.

类被称为UsageStatsManager,我的猜测是,该函数<一href="http://developer.android.com/reference/android/app/usage/UsageStatsManager.html#queryUsageStats(int,%20long,%20long)">queryUsageStats"做这项工作。

The class is called "UsageStatsManager", and my guess is that the function "queryUsageStats" does the job.

此外,似乎它有一个新的权限(android.permission.PACKAGE_USAGE_STATS),这是一个系统的权限,但它写入的是:

Also, it seems it has a new permission ("android.permission.PACKAGE_USAGE_STATS"), which is a system permission, but it's written that:

声明许可意味着不想使用API​​和用户   该设备可以通过设置应用程序许可。

declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.

<一个href="https://developer.android.com/about/versions/android-5.0.html#System">Here's关于这个新功能的另一个链接。

Here's another link about this new functionality.

我看的Andr​​oid code,并注意到背景有USAGE_STATS_SERVICE,其JavaDoc中说,接下来的事情:

I've looked at the code of Android, and noticed that "Context" has USAGE_STATS_SERVICE , which in the JavaDocs say the next thing:

/**
 * Use with {@link #getSystemService} to retrieve a {@link
 * android.app.UsageStatsManager} for interacting with the status bar.
 *
 * @see #getSystemService
 * @see android.app.UsageStatsManager
 * @hide
 */
public static final String USAGE_STATS_SERVICE = "usagestats";

奇怪的是,不仅它说:状态栏,而且还的packageName不匹配(应该是android.app.usage.UsageStatsManager代替)。

The weird thing is that not only it says "status bar", but also the packageName doesn't match (should be "android.app.usage.UsageStatsManager" instead) .

我还添加了正确的权限:

I've also added the correct permission:

<uses-permission
    android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />

和这里的code我用:

and here's the code I use:

  final UsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService("usagestats");// Context.USAGE_STATS_SERVICE);
  final int currentYear=Calendar.getInstance().get(Calendar.YEAR);
  final List<UsageStats> queryUsageStats=usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY,currentYear-2,currentYear);

在模拟器本身,我去设置 - >安全 - >和使用应用程序的访问,并启用了我的应用程序

In the emulator itself, I went to "Settings"->"security"->"apps with usage access" , and enabled my app.

然而,在运行code时,我得到的是一个空的列表...

However, when running the code, all I get is an empty list...

如何使用UsageStatsManager?

How do you use UsageStatsManager ?

另外,你怎么让用户授予在可能的最简单的方法的许可?抑或是自动完成的,只要应用程序试图获取所需要的信息?

Also, how do you let the user to grant the permission in the easiest way possible? Or is it automatically done, as soon as the app tries to get the needed information?

尝试使用这个类时会发生什么事但用户还没有证实它吗?

What happens when trying to use this class yet the user hasn't confirmed it yet?

如何使code返回我的应用程序真正的名单?

How can I make the code return me a real list of apps?

推荐答案

我认为,文件只是短手为日历的东西。我不认为它实际上只有2014年的作品;不过,我可能是错的。

I think the documentation was just short hand for the Calendar stuff. I don't think it actually works with just 2014; however I can be wrong.

为了访问UsageStats的实际列表,你需要用正确的月,日和年创建日历对象。 MRK在对方的回答究竟是怎么说的。我复制并纠正了MRK的code中的错误,这样的人谁看到它在未来能看到它。

In order to access the actually list of UsageStats, you would need to create a Calendar object with the correct month,day, and year. Exactly how MRK said in the other answer. I copied and corrected the errors in MRK's code so anyone who sees it in the future can see it.

Calendar beginCal = Calendar.getInstance();
beginCal.set(Calendar.DATE, 1);
beginCal.set(Calendar.MONTH, 0);
beginCal.set(Calendar.YEAR, 2012);

Calendar endCal = Calendar.getInstance();
endCal.set(Calendar.DATE, 1);
endCal.set(Calendar.MONTH, 0);
endCal.set(Calendar.YEAR, 2014);

final List<UsageStats> queryUsageStats=usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, beginCal.getTimeInMillis(), endCal.getTimeInMillis());

-Credit MRK;由我纠正(他不小心只是把CAL,而不是beginCal和endCal)

-Credit MRK; corrected by me (he accidentally just put cal instead of beginCal and endCal)

在code的使用权限的设置如下。 :)

The code for the usage access settings is below. :)

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);

这篇关于如何使用UsageStatsManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆