Android M detect if permission dialog is visible

ぐ巨炮叔叔 提交于 2020-02-21 10:50:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!