请求PACKAGE_USAGE_STATS的权限 [英] Request permission on PACKAGE_USAGE_STATS

查看:3008
本文介绍了请求PACKAGE_USAGE_STATS的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查用户是否已授予我的应用程序使用 PACKAGE_USAGE_STATS 的权限。我目前正在做的是:

I want to check wheter the user have granted permission for my application to use PACKAGE_USAGE_STATS. What I am currently doing is this:

// Control that the required permissions is instantiated!
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.PACKAGE_USAGE_STATS) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.PACKAGE_USAGE_STATS}, MULTIPLE_PERMISSION_GRANTED);
}

我已按照Android Developer部分的说明实现了requestPermissions,它正在运行适用于 ACCESS_FINE_LOCATION

I have implemented the requestPermissions as described in the Android Developer section and it is working just fine for ACCESS_FINE_LOCATION.

但是当我的应用程序请求权限时,它只显示 ACCESS_FINE_LOCATION 权限,而不是 PACKAGE_USAGE_STATS 任何人都可以解释原因吗?如果是这样,给我一个解决方案,我该怎么做?

But when my application request permissions it only shows the ACCESS_FINE_LOCATION permission and not the PACKAGE_USAGE_STATS can anyone explain why? And if so, give a solution to how I could do it?

我希望这两个权限都显示在如下对话框中:

I want both permissions to show up in a dialog box like this:

推荐答案

很遗憾,您无法申请 PACKAGE_USAGE_STATS 在运行时,就像你使用危险权限一样。用户需要通过Settings应用程序手动授予权限,如 UsageStatsManager 文档

Unfortunately you can't request the PACKAGE_USAGE_STATS at runtime like you do with a dangerous permission. The user need to manually grant the permission through the Settings application as explained in the UsageStatsManager documentation:


此API需要权限
android.permission.PACKAGE_USAGE_STATS,这是系统级
权限,不会授予第三方应用。但是,
声明权限意味着有意使用API​​,设备的用户
可以通过设置应用程序授予权限。

This API requires the permission android.permission.PACKAGE_USAGE_STATS, which is a system-level permission and will not be granted to third-party apps. However, declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.

您可以使用以下代码直接打开具有使用权限活动的应用程序:

You can directly open the Apps with usage access activity with this code:

startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));

这里您可以找到一个基本应用程序示例,其中显示了如何使用应用程序使用情况统计API。

Here you can find an example of a basic app that shows how to use App usage statistics API.

这篇关于请求PACKAGE_USAGE_STATS的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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