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-04-22
  • 款款粽子情,深深敬老人 2019-04-21
  • 【理上网来·辉煌十九大】加强产业融合互动 壮大现代产业体系 2019-04-21
  • 刘嘉玲罕见晒老公 梁朝伟合影厨师抿嘴甜笑 2019-04-21
  • 小学入学面试 上千家长带板凳连夜排队 2019-04-20
  • 看改革开放40年:交通篇——从双腿丈量到抬脚上车 2019-04-20
  • 安徽省委书记批评基层“依法不办事”获各界点赞 2019-04-20
  • 想捞的人多了,为何仅仅房地产部门这么红火?因为人们想捞,发起传销就合理了? 2019-04-20
  • 回复@老老保老张工:没看懂啊?小学老师喊你回去补课…… 2019-04-19
  • 被你们说棕子说馋了,放在冰箱里准备端午吃的几个棕子煮上了下午吃,过两天再买,太不计划经济了,呵呵。 2019-04-19
  • 日本:驻日美军一架F 2019-04-19
  • 回归SUV硬派初心 新一代ix35闪耀2017上海车展 2019-04-18
  • 重庆日报报业集团党委书记管洪祝贺人民日报创刊70周年 2019-04-18
  • 潘峰:用“工匠精神”传承红木文化 2019-04-18
  • 购彩成习惯 体彩大乐透又领走一个14万 2019-04-17
  • 944| 401| 76| 936| 9| 285| 595| 620| 461| 593|