onRequestPermissionsResult未在第一次调用

时间:2017-05-29 10:32:33

标签: java android

我正在尝试在运行时调用权限,但是当我调用该方法时   ActivityCompat.requestPermissions(this,PERMISSIONS_REQ,                     PERMISSIONS_REQUEST);    onRequestPermissionsResult方法不是刚刚在第一次调用,

你知道为什么吗?

这是我的代码

if (Build.VERSION.SDK_INT >= 23) {
      chechPermission();
} else {
            if(AppPrefs.getActionLogout(this)){
                navigateToMain();
            }else
             {
                navigateToLogin();
             }
}

public void chechPermission() {
    Log.i(TAG, "Show button pressed. Checking permission.");
    // BEGIN_INCLUDE(permission)
    // Check if the  permission is already available.
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        // permission has not been granted.
        requestPermission();

    } else {
        // permissions is already available, show the preview.
        Log.i(TAG,
                "permission has already been granted. Displaying preview.");
       // navigateToLogin();
    }
    // END_INCLUDE(cpermission)
}

  /**
 * Requests the  permission.
 * If the permission has been denied previously, a SnackBar will prompt the user to grant the
 * permission, otherwise it is requested directly.
 */
private void requestPermission() {
    Log.i(TAG, "permission has NOT been granted. Requesting permission.");

    // BEGIN_INCLUDE(permission_request)
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            android.Manifest.permission.READ_PHONE_STATE)) {
        // Provide an additional rationale to the user if the permission was not granted
        // and the user would benefit from additional context for the use of the permission.
        // For example if the user has previously denied the permission.
        Log.i(TAG, " permission rationale to provide additional context.");

      /*  ActivityCompat.requestPermissions(this,
                PERMISSIONS_REQ,
                PERMISSIONS_REQUEST);*/
    } else {
        //  permission has not been granted yet. Request it directly.
        ActivityCompat.requestPermissions(this, PERMISSIONS_REQ,
                PERMISSIONS_REQUEST);
    }

最后我的onRequestPermissionsResult方法:

 /**
 * Callback received when a permissions request has been completed.
 */
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    if (requestCode == PERMISSIONS_REQUEST) {
        // BEGIN_INCLUDE(permission_result)
        // Received permission result for  permission.
        Log.i(TAG, "Received response for  permission request.");
        // Check if the only required permission has been granted
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //  permission has been granted, preview can be displayed
            Log.i(TAG, " permission has now been granted. Showing preview.");

            try {
                if(AppPrefs.getActionLogout(this)){
                    navigateToMain();
                }else
                {
                    navigateToLogin();
                }
            } catch (Exception ex) {

            }
        } else {
            Log.i(TAG, " permission was NOT granted.");

        }
        // END_INCLUDE(permission_result)
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

0 个答案:

没有答案