Android-requestPermissions(活动,权限[],requestCode)

时间:2018-09-21 09:01:00

标签: android permissions dialog android-permissions runtime-permissions

我一直在尝试实现运行时权限。 以下是其代码段:

RunTimePermissionsUtil.java

package com.orgzit.android.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

public class RuntimePermissionsUtil {

private static boolean shouldAskPermission() {
    return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
}

private static boolean shouldAskPermission(Context context, String permission) {
    if (shouldAskPermission()) {
        int permissionGrantedResult = ActivityCompat.checkSelfPermission(context, permission);
        return !(permissionGrantedResult == PackageManager.PERMISSION_GRANTED);
    }
    return false;
}

//TODO Karan 2018-09-20 Still need to investigate on the behaviour of storing in SharedPreferences whether I have asked for the permission before or not
public static void checkPermission(Context context, String permission, AskPermissionListener permissionListener) {
    if (shouldAskPermission(context, permission)) {
        if (!ActivityCompat.shouldShowRequestPermissionRationale(((Activity)context), permission)) {
            permissionListener.onNeedPermission();
        } else {
            permissionListener.onPermissionDisabled();
        }
    } else {
        permissionListener.onPermissionGranted();
    }
}

public interface AskPermissionListener {

    void onPermissionGranted();

    void onNeedPermission();

    void onPermissionDisabled();
}
}

在活动 AddFileActivity 中,我在Click事件上实现了此侦听器,其代码是:

public class AddFileActivity extends DrawerActivity {

private static final int CAMERA_REQ_CODE = 1001;
private static final int FILE_SELECT_REQ_CODE = 1002;

private Activity activity;

private RuntimePermissionsUtil.AskPermissionListener askPermissionListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = this;
    init();
    getSupportActionBar().setTitle("Add File");

    askPermissionListener = new RuntimePermissionsUtil.AskPermissionListener() {
        @Override
        public void onPermissionGranted() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraFileUri = getCameraFileUri();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraFileUri);
            startActivityForResult(intent, CAMERA_REQ_CODE);
        }

        @Override
        public void onNeedPermission() {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, CAMERA_REQ_CODE);
        }

        @Override
        public void onPermissionDisabled() {
            Toast.makeText(activity,"App does not have the permission to Open Camera :(", Toast.LENGTH_LONG).show();
        }
    };
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    Log.d("q","q");
}

private void init() {
            showFileChooser();
}


private void showFileChooser() {
    final CharSequence[] items = {"Open camera", "Select from device",
            "Cancel"};
    AlertDialog.Builder builder = new AlertDialog.Builder(AddFileActivity.this);
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Open camera")) {
                RuntimePermissionsUtil.checkPermission(activity, Manifest.permission.CAMERA, askPermissionListener);
                Log.d("d","d");
            } else if (items[item].equals("Select from device")) {
                //DO SOMETHING
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

所以,我所有的代码都执行了,并进入了onNeedPermissions()代码,其中,它进入了

ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, CAMERA_REQ_CODE);

但是,在此之后,令人惊讶的是,没有对话框询问摄像机的运行时权限,而是关闭了该对话框并再次显示活动。

有人可以在这方面帮我吗,或者指出解决方法

1 个答案:

答案 0 :(得分:0)

未显示权限请求对话框的原因是:

  • 已授予该权限。
  • 或者已将权限设置为不再询问。
  • 或者您还没有在清单中声明许可。

在所有情况下,onRequestPermissionsResult()都会立即调用而不会显示对话框。检查它是否被调用,并检查结果。

建议您不要看看自己的实用程序类,而要看看我的库:https://github.com/nabinbhandari/Android-Permissions