在我的应用程序中,我使用request_usage_stats跟踪后台包含的应用程序。我希望用户无需设置即可激活权限。可能吗?如果可以,请帮助我
这是我的自定义警报对话框的屏幕截图
public static AlertDialog show(Context context, Runnable okRunnable, Runnable cancelRunnable) {
View view = LayoutInflater.from(context).inflate(R.layout.dialog_usage_access, null);
AlertDialog alertDialog = new AlertDialog.Builder(context)
.setView(view)
.create();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
View btnOK = view.findViewById(R.id.btnOK);
Switch accessSwitch = view.findViewById(R.id.accessSwitch);
showSwitcherAnimation(accessSwitch);
btnOK.setOnClickListener(view1 -> {
alertDialog.dismiss();
okRunnable.run();
});
alertDialog.show();
return alertDialog;
}
点击“确定”按钮将调用此功能
private void requestStats() {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(intent);
}