当前位置:  开发笔记 > 编程语言 > 正文

如何在DialogFragment中获取Permission结果回调?

如何解决《如何在DialogFragment中获取Permission结果回调?》经验,为你挑选了1个好方法。

这是我的代码示例.

public class DialogPermission extends DialogFragment {

    @Nullable
    @Override
    public View getView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.frg_dialog_permission, null, false);
        Button btnRead = (Button) root.findViewById(R.id.btn_read_contact_permission);
        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
                    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.READ_CONTACTS)){
                    }else{
                      ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_CONTACTS},101);}
                }
            }
        });
        return root;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Log.e("DialogPermission","Ho! Ho! Ho!");  // Log not printed
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

我不希望在Activity中回调.我希望一切都清楚地解释.



1> Dhaval Patel..:

要在同一对话框中获取Request权限结果,请使用DialogFragmentclass的requestPermissions方法.

public class DialogPermission extends DialogFragment {

    @Nullable
    @Override
    public View getView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.frg_dialog_permission, null, false);
        Button btnRead = (Button) root.findViewById(R.id.btn_read_contact_permission);
        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
                    if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)){
                    } else{
                      requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},101);
                    }
                }
            }
        });
        return root;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Log.e("DialogPermission","Ho! Ho! Ho!");  // Log printed
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

推荐阅读
雯颜哥_135
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有