记录一下一些常用的方法
方以后好找 所以拷贝过来
1、获取IP地址和IP所在地(在百度和搜狐上获取的如果这2个都搞不定那完蛋)
/**
1. 获取IP地址和IP所在地
*/
function getIpAdressAndIpGps() {
try {
let ip = ""
let address = ""
let response = http.get('http://pv.sohu.com/cityjson?ie=utf-8');
if (response.statusCode == 200) {
toastLog("sohu")
let InetIP = response.body.string()
eval(InetIP)
ip = (returnCitySN.cip)
address = (returnCitySN.cname)
} else {
toastLog("baidu")
let ip_gps = false
let ip_regx = /本机IP: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}<\/span>([\s\S]*?)<\/td/
let ipUrl = "http://www.baidu.com/s?ie=UTF-8&wd=ip%E5%BD%92%E5%B1%9E%E5%9C%B0%E6%9F%A5%E8%AF%A2" //百度的
let response = http.get(ipUrl);
if (response.statusCode == 200) {
let htmlResult = response.body.string()
ip_gps = ip_regx.exec(htmlResult)
let responseRex = ip_regx.exec(ip_gps)
ip = responseRex[0].split(";")[1].split("</span>")[0]
address = responseRex[1]
}
}
return ip + "@" + address
} catch (error) {
return "获取IP和所在地失败" + "@" + error
}
}
- 获取android手机安装的程序(系统的你自己可以过滤一下我比较懒就没过滤,说实话确实有点慢)
/**
1. 获取手机安装app的集合
*/
function getAppList() {
var pm = context.getPackageManager()
var appList = pm.getInstalledApplications(0)
var appInfoList = []
for (let i = 0; i < appList.size(); i++) {
var app = appList.get(i)
var appInfo = {
appName: app.loadLabel(pm),
packageName: app.packageName,
isSystemApp: app.isSystemApp()
}
if (!app.isSystemApp()) {
appInfoList.push(appInfo)
}
}
return appInfoList
}
- 控件点击公共方法,我认为这个最好节省代码还能随便点点。示例:
用ui选择器也行,用原生的id、text、desc也成,节省N多代码同时兼容性也变得很好,方法判断了 是否存在和是否可点击。
点击控件的方法目前不支持范围Bounds点击,如果点击范围请使用clickBounds(b)。
clickControl(uiSelector("iv_fc_red_close"));
clickControl(id("iv_fc_red_close"));
/**
* 点击屏幕上的坐标
* @param {坐标} b
*/
function clickBounds(b) {
return clickResult = click(b.centerX(), b.centerY());
}
/**
* 点击UI上的控件
* @param {UI上的元素} element
*/
function clickControl(element) {
let clickResult = false;
if (element == null) {
return false;
}
try {
if (element.exists()) {
toastLog("准备click:" + element);
let clickable = element.findOnce().clickable();//是否可以点击
if (clickable) {
clickResult = element.findOnce().click();
sleep(random(2000, 3200));
} else {
let b = element.findOnce().bounds();
clickResult = click(b.centerX(), b.centerY());
sleep(random(2000, 3200));
}
}
return clickResult;
} catch (error) {
toastError("clickControl方法出现错误:" + error)
return false;
}
}
/**
* 元素对象
* @param {ID或name} obj
*/
function uiSelector(obj) {
let element = null;
try {
if (text("" + obj + "").exists()) {
toastLog(text("" + obj + ""))
element = text("" + obj + "");
return element;
}
if (desc("" + obj + "").exists()) {
toastLog(desc("" + obj + ""))
element = desc("" + obj + "");
return element;
}
if (id("" + obj + "").exists()) {
toastLog(id("" + obj + ""))
element = id("" + obj + "");
return element;
}
return element
} catch (error) {
toastError(error);
return null;
}
}
- 强制停止App 我做了各种兼容主流的机器问题不大,目前小米 华为 oppo 魅族 荣耀问题不大。
/**
* 强制停止app
* @param {应用名称} appName
*/
function stopApp(appName) {
try {
openAppSetting(getPackageName(appName));
sleep(3000);
if (className("android.widget.Button").text("强行停止").exists()) {
className("android.widget.Button").text("强行停止").findOnce().click();
} else {
if (text("强行停止").exists()) {
text("强行停止").findOnce().click();
}
}
sleep(3000);
if (className("android.widget.Button").text("确定").exists()) {
className("android.widget.Button").text("确定").findOnce().click();
toastLog(appName + "已经停止!");
}
else {
if (text("强行停止").exists()) {
text("强行停止").findOnce().click();
toastLog(appName + "已经停止!");
} else {
if (text("结束运行").exists()) {
text("结束运行").findOnce().click();
sleep(500);
if (text("确定").exists()) {
text("确定").findOnce().click();
toastLog("MIUI9 Android7" + appName + "已经停止!");
}
}
}
}
} catch (e) {
toastLog(e);
}
}
- 时间函数,获取当天和获取时分秒的
/**
* 获取当前时间格式yyyyMMdd
*/
function getDate() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
if (month < 10) {
month = "0" + month;
};
var day = date.getDate();
if (day < 10) {
day = "0" + day;
};
return year + "-" + month + "-" + day;
}
/**
*
*/
function getTime() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
if (month < 10) {
month = "0" + month;
};
var day = date.getDate();
if (day < 10) {
day = "0" + day;
};
var hour = date.getHours();
if (hour < 10) {
hour = "0" + hour;
};
var minute = date.getMinutes();
if (minute < 10) {
minute = "0" + minute;
};
var seconds = date.getSeconds(); //获取当前秒数(0-59)
var millisecond = date.getMilliseconds(); //获取当前毫秒数(0-999)
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + seconds+":"+millisecond;
}