Android M 权限对话未出现在 fragment 中

标签 android android-fragments android-6.0-marshmallow runtime-permissions

我正在尝试为 android M 请求 READ_PHONE_STATE 权限,但它在 Activity 中有效,当我在 fragment 中实现它时,它不显示对话框。 这是代码。

if (preference.getToken() == null) {
                    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        requestReadPhoneStatePermission();
                    } else {
                        TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
                        HashMap<String, String> params = new HashMap<String, String>();
                        params.put("appCode", Constants.TRACKING_ID);
                        params.put("phone", tm.getDeviceId());
                        DeviceUserService.getDeviceUser(params, getContext());
                        bookmark();
                    }

这是方法 requestReadPhoneStatePermission

public void requestReadPhoneStatePermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_PHONE_STATE)) {

        } else {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE);
        }
    }

这是关于 onRequestPermissionsResult 的代码。

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case READ_PHONE_STATE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getContext(), "granted", Toast.LENGTH_LONG).show();

                } else {
                    Toast.makeText(getContext(), "permission not granted", Toast.LENGTH_LONG).show();
                }
                return;
        }
    }

我错过了什么?提前致谢。

最佳答案

您的 requestReadPhoneStatePermission 方法有误。

编辑

我看到这篇旧帖子得到了支持,并意识到这不是一个正确的实现。

这是处理权限请求的正确方法。

public void requestReadPhoneStatePermission() {
    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE);
}



@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode==READ_PHONE_STATE){
        if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
            //do your thing
        }
        else{
            if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_PHONE_STATE)){
                //user denied the permission but did not check the "never show again" option. 
                //You can ask for the permission again or show a dialog explaining
                //why you need the permission with a button that requests the permission again on click.
            }
            else{
                //user denied the permission and checked the "never show again" option. 
                //Here you can show a dialog explaining the situation and that the user has 
                //to go to the app settings and allow the permission otherwise yor feature 
                //will not be available.
            }
        }
    }

}

关于Android M 权限对话未出现在 fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465148/

相关文章:

java - ListView 项目文本不改变颜色

android - 用于 android getConnectionsfor currentuser() 异常的 Linkedin API

android - 如何在抽屉导航 fragment 中放置两个回收 View

android - 显示摄像头 Intent 仅在Android中的Webview中?无法仅获取Camera Intent?android 5+和6+

android - 带有比例标签的动画列表在 Marshmallow 中不起作用

android - 在 Android 上将图像和文本分享到 Facebook

Android Chrome - 检测网站是否完全向下滚动

android - 嵌套 fragment 在一个选项卡里面?

java - Android, fragment 事务替换的奇怪行为()

android - 在装有 Android 6.0 的 LG 手机上,应用程序在 libicuuc.so 中的 native 代码中崩溃