Permission dialog exits automatically android

与世无争的帅哥 提交于 2021-02-07 10:34:20

问题


I'm working on Marshmallow permission support, and I have implemented it in my app. But I observe a strange behavior while requesting permissions.

PROBLEM:

When I launch my app for the first time, everything goes well, and on the home page, the user is presented with a permission dialog asking for ACCESS_FINE_LOCATION. User denies the permission, and on denial I show another dialog, explaining why the permission is needed, and to reconsider the option of denying the permission (with I'm Sure and Retry options). User presses I'm Sure option, and then exits the app through back press. Now, if the app is again launched, the same activity again tries to ask for the permission, since the user didn't flag 'Never ask again'. But this time, the dialog automatically disappears before the user can make a choice.

CODE TO REQUEST PERMISSION:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(mPermissionsRequired[0]) == PackageManager.PERMISSION_DENIED) {
    PermissionUtils.requestPermissions(this, mPermissionsRequired, KeyConstant.PERMISSION_LOCATION_REQUEST_CODE);
} else {
    ((BBApplication) getApplicationContext()).startLocationUpdates();
}

Callback:

 @TargetApi(Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    Log.v ("gaurav", "Requestcode: " + requestCode + " Permission code: " + KeyConstant.PERMISSION_LOCATION_REQUEST_CODE + " Permission length: " + permissions.length);
    if (requestCode == KeyConstant.PERMISSION_LOCATION_REQUEST_CODE) {
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                boolean showRationale = shouldShowRequestPermissionRationale(permission);
                if (!showRationale) {
                    // user denied flagging NEVER ASK AGAIN
                    // you can either enable some fall back,
                    // disable features of your app
                    // or open another dialog explaining
                    // again the permission and directing to
                    // the app setting

                } else {
                    PermissionUtils.showPermissionDescriptionDialog(this, KeyConstant.PERMISSION_LOCATION_REQUEST_CODE, this);
                }
                // user denied WITHOUT never ask again
                // this is a good place to explain the user
                // why you need the permission and ask if he want
                // to accept it (the rationale)

            } else if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                switch (i) {
                    case 0:
                        ((BBApplication) getApplicationContext()).startLocationUpdates();
                        break;
                }
            }
        }
    }
}

This callback gets fired each time, but in case of automatic disappearance of dialog, I observe permission array in callback to be of zero-length. This, I believe is due to the reason explained in this question: Duplicate permission request after orientation change

But what confuses me is that when the activity is destroyed, and created again, why this behaviour occurs. Also, in logcat upon disappearance of dialog, I see the following log:

12-13 19:31:10.854 779-945/? W/ActivityManager: startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=in.til.popkorn.debug/in.til.popkorn.ui.activities.HomePageActivity (has extras) }

I'm not sure whether this log is relevant to the problem. I'd appreciate some help over this. Please let me know if more code is needed.

UPDATE:

Permission Utils code:

public static void requestPermissions(Activity activity, String[] permissions, int requestCode) {

    // Request permissions.
    ActivityCompat.requestPermissions(activity, permissions, requestCode);
}

回答1:


Deep in my mind I knew that it was some problem with my code, which got confirmed when the issue wasn't reproducible on sample app. There were 2 calls going for starting an activity, hence the newer call always forced the previous permission callbacks to finish. Putting a check on the second call solved my problem.



来源:https://stackoverflow.com/questions/41123400/permission-dialog-exits-automatically-android

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