北京pk10投注站老平台:android权限请求一行代马

96
TMAC_EAH
2018.08.28 10:16* 字数 129
查看rxpermission源码可知,核心代码是创建一个无ui的fragment,fragment是可感知activity生命周期的,这个时候,解决方案可不是跃然纸上了吗,不熟rxjava怎么办,在下封装了个简易代码,仅供参考~
 pk10牛牛app下载 www.y6l57.com.cn public class PermissionCompat {
    public static final int CODE = 200;
    /**
     * 拒绝,不在询问,死了这条心吧~
     */
    public static final int DEATH_REFUSAL = 250;
    /**
     * 一般拒绝,还有机会
     */
    public static final int NORMAL_REFUSAL = 251;
    /**
     * 通过
     */
    public static final int PASS = 201;

    public interface PermissionCompatCallback {
        /**
         * @param cmds PASS 通过|NORMAL_REFUSAL 拒绝 |DEATH_REFUSAL 拒绝(
         */
        void ok(int cmds);

        void refuse(int cmds);

        void goSettings(int cmds);
    }

    public static abstract class PerCompatCallbackAdpt implements PermissionCompatCallback {

        @Override
        public abstract void ok(int cmds);

        @Override
        public void refuse(int cmds) {

        }

        @Override
        public void goSettings(int cmds) {

        }
    }

    public static final String TAG = "fragment";

    /**
     */
    public static void tryReqs(FragmentActivity act, PerCompatCallbackAdpt mPermissionCompatCallback, String... cmds) {

        if (cmds != null && cmds.length > 0) {
            List<String> mCmdLists = new ArrayList<>();
            for (String cmdStr : cmds) {
                mCmdLists.add(cmdStr);
            }
            for (String cmd : mCmdLists) {
                if (ContextCompat.checkSelfPermission(act, cmd) != PackageManager.PERMISSION_GRANTED) {
                    String[] mPermissions = mCmdLists.toArray(new String[mCmdLists.size()]);
                    FragmentManager fm = act.getSupportFragmentManager();
                    ShadowFragment mShadowFragment = (ShadowFragment) fm.findFragmentByTag(TAG);
                    if (mShadowFragment == null) {
                        mShadowFragment = new ShadowFragment();
                        fm.beginTransaction().add(mShadowFragment, TAG).commitAllowingStateLoss();
                    }
                    fm.executePendingTransactions();
                    if (mShadowFragment != null) {
                        mShadowFragment.mPermissionCompatCallback = mPermissionCompatCallback;
                        mShadowFragment.requestPermissions(mPermissions, CODE);
                    }
                    return;
                }
            }
            if (mPermissionCompatCallback != null) {
                mPermissionCompatCallback.ok(PASS);
            }
        } else {
            if (mPermissionCompatCallback != null) {
                mPermissionCompatCallback.ok(PASS);
            }
        }
    }

    public static boolean hasAllPermissionsGranted(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }
}


public class ShadowFragment extends Fragment {
    public PermissionCompat.PermissionCompatCallback mPermissionCompatCallback;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 禁止横竖屏切换时的Fragment的重建
        setRetainInstance(true);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        System.out.println("###########权限回调执行啦#############");
        System.out.println("###########权限回调执行啦###########################");
        System.out.println("###########权限回调执行啦#############");
        if (mPermissionCompatCallback != null) {
            if (PermissionCompat.hasAllPermissionsGranted(grantResults)) {
                mPermissionCompatCallback.ok(PermissionCompat.PASS);
            } else {
                for (String permission : permissions) { //拒绝,并且不在询问
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)) {
                        mPermissionCompatCallback.goSettings(PermissionCompat.DEATH_REFUSAL);
                        return;
                    }
                }
                mPermissionCompatCallback.refuse(PermissionCompat.NORMAL_REFUSAL);
            }
        }
    }
}

activity中,一行代马

      PermissionCompat.tryReqs(this, new PermissionCompat.PerCompatCallbackAdpt() {
            @Override
            public void ok(int cmds) {
                System.out.println("==============权限通过了==============");
            }
            @Override
            public void refuse(int cmds) {
                System.out.println("==============权限被拒绝了==============");
            }

            // 还未添加功能
            @Override
            public void goSettings(int cmds) {
                System.out.println("==============请去setting界面==============");
            }
        }, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE,Manifest.permission.CAMERA);

fragment中,一行代马

PermissionCompat.tryReqs(getActivity(), new PermissionCompat.PermissionCompatCallbackImplAdpt() {

            @Override
            public void ok(int cmds) {
                System.out.println("==============权限通过了老哥==============");
            }
        }, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);

仅供参考,还可以封装的,可视我不会啦~

看看回掉函数名字起的,哎吆我去,,,,,真是又臭又长~

类库地址觉得有用,赏个star,多谢~

日记本
Web note ad 1
  • 改革释放活力 中国经济更具韧性 2019-02-21
  • 【健康解码】儿童发热怎么办? 2019-02-21
  • 国家监察体制改革全国展开 新反腐机构日渐成型 2019-02-21
  • 云南:职业技能比拼展风采 2019-02-21
  • 机关党建工作巡礼——北京“三进两促” 2019-02-20
  • 学习贯彻落实习近平总书记重要讲话精神 2019-02-20
  • 特朗普与金正恩将于6月12日9点举行会晤 2019-02-19
  • 室内烟霾预警急需国家敲响 2019-02-19
  • 您好,这个东西没法说.上次某高校教授写了篇引起轰动.恰巧我碰到他在接头买肉夹镆,并且当时他一口气就吃三个.我毫不客气的说,您能一口气能吃三个肉夹镆就是最大的身份 2019-02-18
  • 走好中国特色社会主义乡村振兴之路 奋力书写新时代山西“三农”工作新篇章(省委书记谈乡村振兴(18)) 2019-02-18
  • 争分夺秒!诉讼时效仅剩28小时 10年前性侵嫌犯被捕 2019-02-17
  • 地震发生前到底有无“前兆”?专家:目前无确定的一一对应现象 2019-02-17
  • 美国的本质和终极目的决定,和中国必定是始终不断战事硝烟。 2019-02-17
  • [尴尬]似乎中国的“推墙带路党没成气候”似的,中兴的屈辱还在眼前! 2019-02-16
  • 认为要彩礼是侮辱 男子与妻子结婚不到15分钟就离婚 2019-02-16
  • 291| 309| 332| 497| 337| 38| 743| 468| 692| 709|