如何允许您的应用使用访问权限?

时间:2016-07-25 17:18:29

标签: android android-permissions

来自android lollipop,需要通过安全性启用想要访问其他应用程序使用的应用程序。而不是通过设置手动启用它 - >安全 - >具有使用权限的应用,我想以编程方式启用它。我无法在任何地方找到明确的答案。任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:2)

在您的活动中尝试此操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!isAccessGranted()) {
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        startActivity(intent);
    }
}


private boolean isAccessGranted() {
    try {
        PackageManager packageManager = getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                applicationInfo.uid, applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);

    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

答案 1 :(得分:1)

试试这段代码我希望能帮到你

if (Build.VERSION.SDK_INT >= 21) {
        UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
        long time = System.currentTimeMillis();
        List stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);

        if (stats == null || stats.isEmpty()) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            context.startActivity(intent);
        }
    }