java - onRequestPermissionsResult 未调用

标签 java android

Marshmallow 运行时权限 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();
    }

说明:

在主代码中,您仅 requestStoragePermission()。

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

如果授予了权限,那就好了,仅此而已。

如果没有授予权限,那么如果是第一次请求权限,shouldShowRequestPermissionRationale()将返回true。然后,我提示另一个对话框并提供一些解释,为用户提供另一次启用权限的机会。

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

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

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

相关文章:

java - 从mysql数据库获取数据并显示在android上

java - 如何对具有对象的 RequestParam 的 Controller 方法进行单元测试?

java - Presto集群+如何根据内存资源调优jvm.config

java - 将 mouseListener 添加到 JPanel 数组

java - 从扩展的 Application 类停止 Android 服务

java - 为什么 RxNetty 使用线程池?

android - flutter doctor --android-licenses 不起作用

android - 测试通过 maven 和 adb 运行,而不是 IntelliJ - 为什么?

android - 如何从动态 ArrayList 为 Android SQLite 查询编写 WHERE IN 子句

android - 如何在每天中午和每次启动时运行服务