onRequestPermissionsResult未调用

时间:2016-09-06 13:41:11

标签: java android

Marshmallow运行时权限onRequestPermissionsResult也没有被调用在运行Playstore Build Apk但正常工作时正常的debuging Apk.Anyone帮助我......谢谢。

这是我的代码

private void EnablePermissions() 
{
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.RECORD_AUDIO)) {

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.RECORD_AUDIO },
                MY_PERMISSIONS_REQUEST_RECORD);
        Toast.makeText(MainActivity.this, "Permission Request", Toast.LENGTH_SHORT).show();
        // result of the request.
    }
    // Add a marker in Sydney, Australia, and move the camera.
    if (ContextCompat.checkSelfPermission(MainActivity.this,
            android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(MainActivity.this, "checkSelfPermission ", Toast.LENGTH_SHORT).show();
        return;
    } else {
        Log.d("Permission Denied", "Permission Failed to enable");
        Toast.makeText(MainActivity.this, "Permission Failed to enable", Toast.LENGTH_SHORT).show();
    }
}

OnRequestPermissionResult方法

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    try {
        switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_RECORD: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                ChatActivity();

            } else {
                Toast.makeText(MainActivity.this, "Permission denied Chat ", Toast.LENGTH_SHORT).show();
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
        }

    } catch (Exception e) {
        Log.e(Constants.LOG, e.getMessage());
    }
}

1 个答案:

答案 0 :(得分:0)

这是正确的做法https://stackoverflow.com/a/35495372/4493133。我在下面做的基本相同。

以下是我如何使其适用于我的代码的示例

private void requestStoragePermission() {
        /*
         We don't have permission so prompt the user
         If permission is not granted control goes to onRequestPermissionResult
          */
        ActivityCompat.requestPermissions(
                this,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }

onRequestPermissionsResult方法:

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        switch (requestCode) {
            case REQUEST_EXTERNAL_STORAGE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d("PERMISSION", "Storage permission granted");
                } else {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                        showMessageOkCancel("You need to allow access to Storage to use the offline timetables",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        switch (which) {
                                            case DialogInterface.BUTTON_POSITIVE:
                                                requestStoragePermission();
                                                break;
                                            case DialogInterface.BUTTON_NEGATIVE:
                                                Toast.makeText(MainActivity.this, "Storage permission required to continue", Toast.LENGTH_LONG)
                                                        .show();
                                                finishAffinity();
                                                break;
                                        }
                                    }
                                });
                    } else {
                        Toast.makeText(this, "Go to settings and enable storage permissions", Toast.LENGTH_LONG)
                                .show();
                        finishAffinity();
                    }
                }

        }

    }

showMessageOkCancel方法:

private void showMessageOkCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", okListener)
                .create()
                .show();
    }

说明:

在您的主要代码中,您只需requestStoragePermission()。

无论是否授予权限,

onRequestPermissionsResult都将运行。

如果授予了权限,那就好了,就是这样。

如果未授予权限,则ifShowRequestPermissionRationale()将在第一次请求权限时返回true。然后,我提示另一个带有一些解释的对话框,为用户提供另一个启用权限的机会。

但如果选中“不再询问”,则shouldShowRequestPermissionRationale将返回false。 requestPermissions对话框不再弹出。用户必须转到手动启用权限

请注意,您不必使用finishAffinity()。您可以禁用需要该权限的代码部分。