×

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
  • 脸每天都洗 但你真的洗对了吗? 2018-12-16
  • 靠拢美国失算!国内突然大乱,越南内部冲突升级,民众苦不堪言 2018-12-16
  • 俄罗斯世界杯本周四开锣 大力神杯最终花落谁家 2018-12-16
  • 世界杯魔咒应验 A股3000点命悬一线 2018-12-15
  • [大笑]你这头猪看得懂逻辑? 2018-12-15
  • “男童在幼儿园噎食死亡”,信息渠道应当畅通无阻 2018-12-15
  • Monopoly practices may backfire on Chinas mobile economy 2018-12-14
  • 【新媒体矩阵】长城论坛微信公众号 2018-12-14
  • 货币政策取向仍以国内为主 2018-12-14
  • 靠拢美国失算!国内突然大乱,越南内部冲突升级,民众苦不堪言 2018-12-14
  • 一村一品!安徽推广“四带一自”产业扶贫模式 2018-12-13
  • 北京某公司为办张学友演唱会伪造宣传部邀请函 文化和旅游部:伪造者已遭刑拘 2018-12-13
  • 《堡垒之夜》Switch版下载量:一天破200万次 2018-12-13
  • 个税法迎第七次大修 起征点调至每年6万元 2018-12-12
  • 深圳晚报总编辑丁时照祝贺人民日报创刊70周年 2018-12-12
  • 105| 764| 589| 240| 611| 681| 592| 28| 143| 246|