系列文章目录
APK禁止在模拟器上使用
前言
近些年一直从事Android开发,没养成好习惯,也一直没想起来写博客,几年前注册的 到现在空空如也,最近准备加入博客专区
一、为何要禁止模拟器安装?
具体的我也不是很清楚,简单的来说就是防止修改一些手机上不能修改出来的东西去达到一些不良用户的需求,例如更改定位,截获和篡改消息,恶意捣乱等等。
二、以往解决思路
1.判断蓝牙是否有效,获取蓝牙名字判断。
2.判断是否存在光传感器,重力感应器。
3.通过是否能拨打电话 。
4.电池状态与电量和充电状态、电池温度。
5.去判断SIM卡,获取运行商信息。
6.获取cpu型号判断。
7.获取 imei 是否不为“000000000000000”。
8.wifi,GPS,温度传感器。
(代码在这里我就不个个沾了,随便搜索都有的)
以上的方法我都实验过 确实可以防住很多模拟器, 后来同事反馈夜神还能正常跑,额~~,于是我就去实验了一下,确实能跑,而且夜神还能用微信、淘宝、京东、支付宝、甚至银行的app都能搞,顿时觉得,夜神真牛逼
三、最终方案
分析了上面的解决办法,其实觉得有些手机都没有什么重力感应器,温度传感器,这样搞了也会对部分正常手机用户都是用不了了,而且最重要的问题就是判断安卓模拟器本身就不一定好使,于是我慢慢琢磨,为什么要死扣模拟器和真机的区别呢?
于是我尝试换一条思路解决问题,偶然间发现模拟器提供的应用商店是不让卸载的,这也就意味着存在某个应用商店,大概率就是该模拟器了
于是我查阅了个个模拟器的官网,也查了查百度终于找到方案了
package 自己项目的包名;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.text.TextUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class CheckSimulator {
private static final String[] PKG_NAMES = {"com.mumu.launcher", "com.ami.duosupdater.ui", "com.ami.launchmetro", "com.ami.syncduosservices", "com.bluestacks.home", "com.bluestacks.windowsfilemanager", "com.bluestacks.settings", "com.bluestacks.bluestackslocationprovider", "com.bluestacks.appsettings", "com.bluestacks.bstfolder", "com.bluestacks.BstCommandProcessor", "com.bluestacks.s2p", "com.bluestacks.setup", "com.bluestacks.appmart", "com.kaopu001.tiantianserver", "com.kpzs.helpercenter", "com.kaopu001.tiantianime", "com.android.development_settings", "com.android.development", "com.android.customlocale2", "com.genymotion.superuser", "com.genymotion.clipboardproxy", "com.uc.xxzs.keyboard", "com.uc.xxzs", "com.blue.huang17.agent", "com.blue.huang17.launcher", "com.blue.huang17.ime", "com.microvirt.guide", "com.microvirt.market", "com.microvirt.memuime", "cn.itools.vm.launcher", "cn.itools.vm.proxy", "cn.itools.vm.softkeyboard", "cn.itools.avdmarket", "com.syd.IME", "com.bignox.app.store.hd", "com.bignox.launcher", "com.bignox.app.phone", "com.bignox.app.noxservice", "com.android.noxpush", "com.haimawan.push", "me.haima.helpcenter", "com.windroy.launcher", "com.windroy.superuser", "com.windroy.launcher", "com.windroy.ime", "com.android.flysilkworm", "com.android.emu.inputservice", "com.tiantian.ime", "com.microvirt.launcher", "me.le8.androidassist", "com.vphone.helper", "com.vphone.launcher", "com.duoyi.giftcenter.giftcenter"};
private static final String[] PATHS = {"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "/system/lib/libc_malloc_debug_qemu.so", "/sys/qemu_trace", "/system/bin/qemu-props", "/dev/socket/qemud", "/dev/qemu_pipe", "/dev/socket/baseband_genyd", "/dev/socket/genyd"};
private static final String[] FILES = {"/data/data/com.android.flysilkworm", "/data/data/com.bluestacks.filemanager"};
public static boolean isSimulator(Context paramContext) {
try {
List pathList = new ArrayList();
pathList = getInstalledSimulatorPackages(paramContext);
if (pathList.size() == 0) {
for (int i = 0; i < PATHS.length; i++)
if (i == 0) {
if (new File(PATHS[i]).exists()) continue;
pathList.add(Integer.valueOf(i));
} else {
if (!new File(PATHS[i]).exists()) continue;
pathList.add(Integer.valueOf(i));
}
}
if (pathList.size() == 0) {
pathList = loadApps(paramContext);
}
return (pathList.size() == 0 ? null : pathList.toString()) != null;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static List getSimulatorInfo(Context paramContext) {
List pathList = new ArrayList();
List simulatorMaps = new ArrayList();
try {
pathList = getInstalledSimulatorPackages(paramContext);
String brand = getSimulatorBrand(pathList);
if (TextUtils.isEmpty(brand)) {
List<String> list = loadApps(paramContext);
if (list.size() > 0) {
simulatorMaps.add(list.get(0));
}
} else {
simulatorMaps.add(brand);
}
} catch (Exception e) {
e.printStackTrace();
}
return simulatorMaps;
}
private static List getInstalledSimulatorPackages(Context context) {
ArrayList localArrayList = new ArrayList();
try {
for (int i = 0; i < PKG_NAMES.length; i++)
try {
context.getPackageManager().getPackageInfo(PKG_NAMES[i], 1);
localArrayList.add(PKG_NAMES[i]);
} catch (PackageManager.NameNotFoundException localNameNotFoundException) {
}
if (localArrayList.size() == 0) {
for (int i = 0; i < FILES.length; i++) {
if (new File(FILES[i]).exists())
localArrayList.add(FILES[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return localArrayList;
}
public static List loadApps(Context context) {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<String> list = new ArrayList<>();
List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(intent, 0);
//for循环遍历ResolveInfo对象获取包名和类名
for (int i = 0; i < apps.size(); i++) {
ResolveInfo info = apps.get(i);
String packageName = info.activityInfo.packageName;
CharSequence cls = info.activityInfo.name;
CharSequence name = info.activityInfo.loadLabel(context.getPackageManager());
if (!TextUtils.isEmpty(packageName)) {
if (packageName.contains("bluestacks")) {
list.add("蓝叠");
return list;
}
}
}
return list;
}
private static String getSimulatorBrand(List<String> list) {
if (list.size() == 0)
return "";
String pkgName = list.get(0);
if (pkgName.contains("mumu")) {
return "mumu";
} else if (pkgName.contains("ami")) {
return "AMIDuOS";
} else if (pkgName.contains("bluestacks")) {
return "蓝叠";
} else if (pkgName.contains("kaopu001") || pkgName.contains("tiantian")) {
return "天天";
} else if (pkgName.contains("kpzs")) {
return "靠谱助手";
} else if (pkgName.contains("genymotion")) {
if (Build.MODEL.contains("iTools")) {
return "iTools";
} else if ((Build.MODEL.contains("ChangWan"))) {
return "畅玩";
} else {
return "genymotion";
}
} else if (pkgName.contains("uc")) {
return "uc";
} else if (pkgName.contains("blue")) {
return "blue";
} else if (pkgName.contains("microvirt")) {
return "逍遥";
} else if (pkgName.contains("itools")) {
return "itools";
} else if (pkgName.contains("syd")) {
return "手游岛";
} else if (pkgName.contains("bignox")) {
return "夜神";
} else if (pkgName.contains("haimawan")) {
return "海马玩";
} else if (pkgName.contains("windroy")) {
return "windroy";
} else if (pkgName.contains("flysilkworm")) {
return "雷电";
} else if (pkgName.contains("emu")) {
return "emu";
} else if (pkgName.contains("le8")) {
return "le8";
} else if (pkgName.contains("vphone")) {
return "vphone";
} else if (pkgName.contains("duoyi")) {
return "多益";
}
return "";
}
}
然后在项目需要的地方调用
List simulatorInfo = CheckSimulator.getSimulatorInfo(this);
for (int i = 0; i < simulatorInfo.size(); i++) {
Log.e(TAG, "模拟器型号:"+simulatorInfo.get(i) );
}
如果是模拟器的话,就可以直接显示出来是什么牌子的模拟器了
总结
无论怎样实现,有用才是硬道理,这么做可以防止 PKG_NAMES 里面包名所含有的全部模拟器,如果有新出模拟器版本再去官网查包名就好了好了这样的话可以防止100%的模拟器了
第一次写博客,写的不是很好,如果有缺点还请指点
内容也是看了其他博客收集汇总的,如有侵权请联系删除
QQ:320518219