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方式获取未知来源的状态或修改状态
网友评论