ThanosFisherman
ThanosFisherman

Reputation: 5859

Android M detect if permission dialog is visible

How can I determine programmatically if a permission dialog is visible to the user so I know what to do in this case?

Upvotes: 10

Views: 4178

Answers (2)

Roman Trokhymets
Roman Trokhymets

Reputation: 1102

@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
        }
}`

`

Upvotes: 3

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

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.

Upvotes: 2

Related Questions