requestPermission未显示在支持片段中

时间:2016-05-05 03:10:15

标签: android android-fragments android-appcompat

我知道这个问题已被问过很多次,但这些方法都没有对我有用。我有一个v4.nested片段,我正在调用requestPermission,但我甚至没有显示对话框,因此onRequestPermissionsResult没有被调用。我尝试在我的Activity和Fragment中调用onRequestPermissionsResult,但没有任何工作。我正在使用appcompat-v7:23.3.0所以我期待对话,因此结果。有帮助吗?感谢

代码

public void ask(){
    Log.e("Here", "Permission");
    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) ==
                    PackageManager.PERMISSION_GRANTED) {
        Log.e("Here", "Granted");

        if (what == 1){
            PackageManager pm = getActivity().getPackageManager();
            if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
                Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                getActivity().startActivityForResult(i, CAMERA_PIC_REQUEST);
            } else {
                Toast.makeText(getActivity(), "Can't detect Camera on device", Toast.LENGTH_LONG).show();
            }
        }
        else{
            Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            getActivity().startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    }
    else{
        Log.e("Here", "Ask");
        if (useRuntimePermissions()) {
            if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                showMessageOKCancel("You need to allow access to Device Storage",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 3);
                                Log.e("A", "ash");
                            }
                        });
            } else {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
                Log.e("B", "Should be Asked by now");
            }
        }
    }
}

private boolean useRuntimePermissions() {
    return(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
    new android.support.v7.app.AlertDialog.Builder(getActivity())
            .setMessage(message)
            .setPositiveButton("OK", okListener)
            .setNegativeButton("Cancel", null)
            .create()
            .show();
}

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
   Log.e("Hehe", "results"+String.valueOf(requestCode));
    switch (requestCode) {
        case CAM_REQUEST: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (what == 1){
                    PackageManager pm = getActivity().getPackageManager();
                    if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
                        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        getActivity().startActivityForResult(i, CAMERA_PIC_REQUEST);
                    } else {
                        Toast.makeText(getActivity(), "Can't detect Camera on device", Toast.LENGTH_LONG).show();
                    }
                }
                else{
                    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    getActivity().startActivityForResult(i, RESULT_LOAD_IMAGE);
                }
            }
            else {
                Toast.makeText(getActivity(), "Camera Request not granted. Please enable it at App Settings", Toast.LENGTH_LONG).show();
            }
            return;
        }
    }
}

从按钮tapClickListener调用ask()方法,LogA和LogB正在记录,但权限对话框不显示。

0 个答案:

没有答案