[英]How to get Usage Access Permission programmatically
My app needs to have Usage Access Permission in order to get information about the current running app on user's phone. 我的应用程序需要具有使用权限访问权限才能获得有关用户手机上当前正在运行的应用程序的信息。 I am able to successfully implement it using the following code with the help from the following link. 我可以使用以下代码在以下链接的帮助下成功实现它。
Here is my working code 这是我的工作代码
public void showDialog()
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
@SuppressWarnings("WrongConstant")
UsageStatsManager usm = (UsageStatsManager) getSystemService("usagestats");
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
time - 1000 * 1000, time);
if (appList.size()==0)
{
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Usage Access")
.setMessage("App will not run without usage access permissions.")
.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onClick(DialogInterface dialog, int which) {
// continue with delete
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
// intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Settings$SecuritySettingsActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent,0);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
} else {
Intent intent = new Intent(this, PackageService.class);
startService(intent);
finish();
}
}else
{
Intent intent = new Intent(this, PackageService.class);
startService(intent);
finish();
}
}
This code will show a user an alert dialog that Usage Access permission is required and will take user to the settings screen from where it can be enabled. 此代码将向用户显示一个警告对话框,该对话框需要使用“使用权限”权限,并将用户带到可以启用它的设置屏幕。
Everything works perfectly with this. 一切都与此完美配合。 Only thing that I want is that how to give my app usage access permission by default without showing any dialog to user until he/she doesn't turn it off manually? 我想要的唯一的事情就是如何默认情况下给予我的应用程序使用权限访问权限而不向用户显示任何对话框 ,直到他/她没有手动将其关闭?
You can simply give this Permission on the manifest, ignoring just this permission error: 您只需在清单上提供此权限,而忽略此权限错误:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions"/>
Than use Following code for permission 使用以下代码获取许可
@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 = 0;
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.KITKAT) {
mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
applicationInfo.uid, applicationInfo.packageName);
}
return (mode == AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.