美文网首页
android权限请求一行代马

android权限请求一行代马

作者: TMAC_EAH | 来源:发表于2018-08-28 10:16 被阅读0次
    查看rxpermission源码可知,核心代码是创建一个无ui的fragment,fragment是可感知activity生命周期的,这个时候,解决方案可不是跃然纸上了吗,不熟rxjava怎么办,在下封装了个简易代码,仅供参考~
    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,多谢~

    相关文章

      网友评论

          本文标题:android权限请求一行代马

          本文链接:https://www.haomeiwen.com/subject/qrkfwftx.html