在uniaapp/H5+的应用中如何检测手机端是否被ROOT/检测是否在模拟器上运行/防止截屏
检测手机端是否被ROOT
function isRoot() {
let isRoot = false;
const paths = [
'/sbin/su',
'/system/bin/su',
'/system/xbin/su',
'/data/local/xbin/su',
'/data/local/bin/su',
'/system/sd/xbin/su',
'/system/bin/failsafe/su',
'/data/local/su'
];
var File = plus.android.importClass('java.io.File');
for (let i = 0; i < paths.length; i++) {
let path = paths[i];
var fd = new File(path);
if (fd.exists()) {
isRoot = true;
break;
}
}
if (isRoot) { //检测手机已被root
/*uni.showToast({
title:'检测设备已被ROOT',
icon: 'none',
duration: 2000
});
setTimeout(function(){
plus.runtime.quit();
},1000)*/
uni.showModal({
title: '提示',
content: '检测到设备已被ROOT,如仍然坚持要以ROOT过的终端操作APP,自己承担风险和责任,是否继续操作?',
success: function(res) {
if (res.confirm) {
} else if (res.cancel) {
plus.runtime.quit();
}
}
});
}
}
检测是否在模拟器上运行
function checkSimulator() { //检测是否模拟器
var File = plus.android.importClass('java.io.File');
var isSimulator = false;
var paths = ["/dev/socket/qemud", "/dev/qemu_pipe", "/system/lib/libc_malloc_debug_qemu.so", "/sys/qemu_trace",
"/system/bin/qemu-props", "/dev/socket/genyd", "/dev/socket/baseband_genyd"
];
for (let i = 0; i < paths.length; i++) {
let path = paths[i];
var fd = new File(path);
if (fd.exists()) {
isSimulator = true;
}
}
if (isSimulator) {
uni.showToast({
title: '禁止应用在模拟器上运行',
icon: 'none',
duration: 2000
});
setTimeout(function() {
plus.runtime.quit();
}, 2000)
}
return false;
}
防止截屏
//方法一
var main = plus.android.runtimeMainActivity();
var window = plus.android.importClass("android.view.Window");
main.getWindow().addFlags(0x00002000);
//方法二
var activity = plus.android.runtimeMainActivity();
plus.android.invoke(plus.android.invoke(activity, "getWindow"), "addFlags", 0x00002000);
WebView防止截屏
webView.evalJS('try{document.addEventListener("plusready", function(){ var activity = plus.android.runtimeMainActivity(); plus.android.invoke(plus.android.invoke(activity,"getWindow"),"addFlags",0x00002000); }, false );}catch(e){}');