重新启动时应用于应用程序的授予权限

时间:2016-08-18 09:05:33

标签: android

我使用以下代码检查权限:

if (getActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED || getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                            getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                                    != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE,  Manifest.permission.ACCESS_FINE_LOCATION},
                                MY_PERMISSIONS);
                    }

按照下面的代码我抓住了结果:

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

        if(requestCode == MY_PERMISSIONS){
            if(grantResults[0] != PackageManager.PERMISSION_GRANTED ||
                    grantResults[1] != PackageManager.PERMISSION_GRANTED){
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage(R.string.per_check_m);
                builder.setCancelable(false);
                builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                                            Manifest.permission.WRITE_EXTERNAL_STORAGE,  Manifest.permission.ACCESS_FINE_LOCATION},
                                    MY_PERMISSIONS);
                        }
                    }
                });
                builder.show();
            }else {
                startActivityForResult(com.media_iran.android.iranama.Utils.ImagePicker.getPickImageIntent(getActivity()), INTENT_REQUEST_GET_IMAGES);
            }
        }

    }

但是当用户接受权限时,它不起作用,需要重新启动应用程序,并从应用程序历史记录中清除它以获得工作权限。

我发现它是android中的一个错误,但是通过这种方式其他应用程序如何工作呢? 有没有解决方案?

2 个答案:

答案 0 :(得分:0)

尝试以下代码,这对我来说非常适合。 在OnCreate下面插入代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int hasWriteContactsPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS);
            if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        REQUEST_CODE_ASK_PERMISSIONS);
            }
        }

覆盖onRequestPermissionResult:

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted add code when permission granted.
                    signIn();
                } else {
                    // Permission Denied
                    Toast.makeText(MainActivity.this, "Permission Denied.", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

答案 1 :(得分:0)

在活动中抓住onRequestPermissionsResult()

在您的片段活动中调用

onRequestPermissionsResult()