问题
How can I determine programmatically if a permission dialog is visible to the user so I know what to do in this case?
回答1:
@Override
protected void onStart() {
super.onStart();
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
if ("com.android.packageinstaller.permission.ui.GrantPermissionsActivity".equals(cn.getClassName())){
//permission dialog is displayed
}
}`
`
回答2:
Permission dialog is an activity that is put on top of the activity stack. So when you call for requestPermission()
method Activity
implementation asks the PackageManager
to build intent that will start this activity dialog. This intent has ACTION_REQUEST_PERMISSIONS
action.
Probably you need to listen for activity stack changes and check if activity intent has ACTION_REQUEST_PERMISSIONS
action. I'm not sure if obtaining running tasks will give you this activity listed, because I haven't tried this myself, only to get you going.
来源:https://stackoverflow.com/questions/34235835/android-m-detect-if-permission-dialog-is-visible