android 6.0运行时权限不起作用

时间:2017-05-01 05:37:35

标签: android runtime-permissions

我在android 6.0上第一次处理运行时权限,这是我的代码:

 String[] permissionsRequired = new String[]{Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_EXTERNAL_STORAGE};

private void checkPermission() {
    if (ActivityCompat.checkSelfPermission(getContext(), permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(getContext(), permissionsRequired[1]) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(getContext(), permissionsRequired[2]) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(getContext(), permissionsRequired[3]) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[0])
                || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[1])
                || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[2])
                || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[3])) {

            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Need Multiple Permissions");
            builder.setMessage("This app needs Camera and Location permissions.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    ActivityCompat.requestPermissions(getActivity(), permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else if (permissionStatus.getBoolean(permissionsRequired[0], false)) {
            //Previously Permission Request was cancelled with 'Dont Ask Again',
            // Redirect to Settings after showing Information about why you need the permission
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Need Multiple Permissions");
            builder.setMessage("This app needs Camera and Location permissions.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    sentToSettings = true;
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getContext().getPackageName(), null);
                    intent.setData(uri);
                    startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                    Toast.makeText(getContext(), "Go to Permissions to Grant  Camera and Location", Toast.LENGTH_LONG).show();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else {
            //just request the permission
            ActivityCompat.requestPermissions(getActivity(), permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
        }


        permissionStatus = getContext().getSharedPreferences("permission status", 0);
        SharedPreferences.Editor editor = permissionStatus.edit();
        editor.putBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE, true);
        editor.apply();


    } else {
        //You already have the permission, just go ahead.
        proceedAfterPermission();
    }
}

private void proceedAfterPermission() {
    Toast.makeText(getContext(), "We got All Permissions", Toast.LENGTH_LONG).show();
}

@Override
public void onResume() {
    super.onResume();
    if (sentToSettings) {
        if (ActivityCompat.checkSelfPermission(getContext(), permissionsRequired[0]) == PackageManager.PERMISSION_GRANTED) {
            //Got Permission
            proceedAfterPermission();
        }
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == PERMISSION_CALLBACK_CONSTANT) {
        //check if all permissions are granted
        boolean allgranted = false;
        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                allgranted = true;
            } else {
                allgranted = false;
                break;
            }
        }

        if (allgranted) {
            proceedAfterPermission();
        } else if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[0])
                || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[1])
                || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[2])
                || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[3])) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Need Multiple Permissions");
            builder.setMessage("This app needs Camera and Location permissions.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    ActivityCompat.requestPermissions(getActivity(), permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else {
            Toast.makeText(getContext(), "Unable to get Permission", Toast.LENGTH_LONG).show();
        }
    }
}

我还定义了相机,录制音频,读取和放大;在清单中写入外部存储权限。

   <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

这有什么问题?

Menifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saubhagyam.videouploadingdemo">

<uses-sdk android:minSdkVersion="10" />

<uses-feature android:name="android.hardware.camera2" />

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">

    </activity>
    <activity android:name=".Bio"></activity>
    <activity android:name=".MyCameraRecorder"></activity>
    <activity android:name=".Container">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:0)

您可以在下面使用

ActivityCompat.requestPermissions(getActivity(), permissionsRequired, PERMISSION_CALLBACK_CONSTANT);

ActivityCompat.checkSelfPermission(getContext(), permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED

ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[0])

更改为

requestPermissions(permissionsRequired, PERMISSION_CALLBACK_CONSTANT);

context.checkSelfPermission(permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED

shouldShowRequestPermissionRationale(permissionsRequired[0])

您的上下文,例如片段Context context = getActivity();和活动Context context = MainActivity.this;