Android应用程序不要求相机权限

时间:2016-10-10 15:33:46

标签: java android android-manifest

我正在开发第三方可以使用的SDK(.aar)。在我使用aar的示例应用程序中,我可以看到应用程序没有提示相机权限。当我打开aar并看到它的manifest.xm时,它包含以下内容:

<!-- WRITE_EXTERNAL_STORAGE is needed because we are storing there the config files of KM -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.read_external_storage" />
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />
<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />

由于相机作为必需的权限存在,任何人都可以告诉我为什么在安装示例应用程序时不会出现。

2 个答案:

答案 0 :(得分:2)

从Android 6.0开始,您必须在运行时请求permssions,并且在安装时不会提示用户。

https://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:0)

以下是我检查权限的方法:

    public boolean permissionsGranted() {

    boolean allPermissionsGranted = true;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        boolean hasWriteExternalPermission = (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
        boolean hasReadExternalPermission = (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
        boolean hasCameraPermission = (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
        if (!hasWriteExternalPermission || !hasReadExternalPermission || !hasCameraPermission) {
            allPermissionsGranted = false;
        }
    }
    return allPermissionsGranted;
}

如果permissionsGranted()方法返回false

,这就是我发出请求的方式
 String[] permissions = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE};
                        ActivityCompat.requestPermissions(this, permissions, 1);

你必须覆盖onRequestPermissionsResult方法,下面是我的内容。

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1: {
            boolean startActivity = true;
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults.length > 0 && grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    String permission = permissions[i];
                    boolean showRationale = shouldShowRequestPermissionRationale(permission);
                    if (!showRationale) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
                        intent.setData(uri);
                        startActivityWithIntent(intent);
                        Toast.makeText(this, "Enable required permissions", Toast.LENGTH_LONG).show();
                        startActivity = false;
                        break;
                    } else {
                        Toast.makeText(this, "Enable required permissions", Toast.LENGTH_LONG).show();
                        break;
                    }
                }
            }
            if(startActivity) {
                getActivity().finish();
                startActivityWithIntent(getIntent());
            }
        }
    }
}

public void startActivityWithIntent(Intent intent){
    startActivity(intent);
}

showRationale是检查用户是否打勾再也不询问,如果是,那么我启动设置活动,以便用户可以启用权限。

如果您需要更清晰,请告诉我