onRequestPermissionsResult

onRequestPermissionsResult

棉花糖运行时权限onRequestPermissionsResult在运行playstore Build Apk时也未调用,但可以正常调试Apk正常工作。任何人都可以帮助我..谢谢。

这是我的代码

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());
    }
}

最佳答案

这是正确的方法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();
    }


说明:

在您的主代码中,您仅需要StorageStoragePermission()。

无论是否授予权限,onRequestPermissionsResult都将运行。

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

如果未授予许可,则如果是第一次请求许可,则shouldShowRequestPermissionRationale()将返回true。然后,我提示另一个带有一些说明的对话框,以给用户另一个启用权限的机会。

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

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

关于java - onRequestPermissionsResult未调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39350514/

10-09 03:42