Android中系统应用适配未知来源的管理权限

Android中为了防止一些不良应用的随便静默安装,设置了未知来源应用的开关,在不同的android版本中使上会有所区别

 /**
     * 打开未知来源权限
     * @param context
     */
    public static void openNonMarketAppsPerm(Context context){
        if (Build.VERSION.SDK_INT < 17) {
            int flag = Settings.Secure.getInt(context.getContentResolver(),
                    Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
            if(flag == 0){
                Settings.Secure.putInt(context.getContentResolver(),
                        Settings.Secure.INSTALL_NON_MARKET_APPS, 1);
            }
        } else {
            int flag = Settings.Global.getInt(context.getContentResolver(),
                    Settings.Global.INSTALL_NON_MARKET_APPS, 0);
            if(flag == 0){
                Settings.Global.putInt(context.getContentResolver(),
                        Settings.Global.INSTALL_NON_MARKET_APPS, 1);
            }
        }
    }
 /**
     * 关闭未知来源权限
     * @param context
     */
    public static void closeNonMarketAppsPerm(Context context){
        if (Build.VERSION.SDK_INT < 17) {
            int flag = Settings.Secure.getInt(context.getContentResolver(),
                    Settings.Secure.INSTALL_NON_MARKET_APPS, 1);
            if(flag == 1) {
                Settings.Secure.putInt(context.getContentResolver(),
                        Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
            }
        } else {
            int flag= Settings.Global.getInt(context.getContentResolver(),
                    Settings.Global.INSTALL_NON_MARKET_APPS, 1);
            if (flag==1){
                Settings.Global.putInt(context.getContentResolver(),
                        Settings.Global.INSTALL_NON_MARKET_APPS, 0);
            }
        }
    }

    /**
     * 检查状态是否打开
     * @param context
     */
    public static boolean checkNonMarketAppsPermStatus(Context context){
        boolean unKnowSource=false;
        if (Build.VERSION.SDK_INT<17){
            unKnowSource=Settings.Secure.getInt(context.getContentResolver(),
                    Settings.Secure.INSTALL_NON_MARKET_APPS, 0)==1;
        }else{
            unKnowSource=Settings.Global.getInt(context.getContentResolver(),
                    Settings.Global.INSTALL_NON_MARKET_APPS, 0)==1;
        }
        return unKnowSource;
    }

如果兼容4.0以下版本则需要把Settings.Secure 和Settings.Global 改为:Settings.System方式获取未知来源的状态或修改状态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值