注意: Q群:209689138
这个是低版本的autojs4.1.1的写法高版本自己找实例作参考
第一步:在app中打包你的apk main.js中加入这个代码ip地址的静态资源要填写正确保证能够下载 每次更新代码更新服务器中的静态资源即可
使用静态网络资源下载脚本 实现手机打开软件热更新
try {
var ceshicode="http://127.0.0.1/app/douyin.js"
let filePath = files.join(files.cwd(), 'douyin.js');
download(ceshicode, filePath);
try {
engines.execScriptFile(filePath);
} catch (error) {
toastLog(error)
}
} catch (error) {
toastLog(error)
}
function download(url, filePath) {
log(filePath);
importClass('java.io.FileOutputStream');
importClass('java.io.IOException');
importClass('java.io.InputStream');
importClass('java.net.MalformedURLException');
importClass('java.net.URL');
importClass('java.net.URLConnection');
importClass('java.util.ArrayList');
var url = new URL(url);
var conn = url.openConnection(); //URLConnection
var inStream = conn.getInputStream(); //InputStream
var fs = new FileOutputStream(filePath); //FileOutputStream
var connLength = conn.getContentLength(); //int
var buffer = util.java.array('byte', 1024); //byte[]
var byteSum = 0; //总共读取的文件大小
var byteRead; //每次读取的byte数
log('要下载的文件大小=');
log(connLength);
var threadId = threads.start(function() {
while (1) {
var 当前写入的文件大小 = byteSum;
var progress = (当前写入的文件大小 / connLength) * 100;
if (progress > 0.1) {
var progress = parseInt(progress).toString() + '%';
if (当前写入的文件大小 >= connLength) {
break;
}
}
sleep(1000);
}
});
while ((byteRead = inStream.read(buffer)) != -1) {
byteSum += byteRead;
//当前时间
currentTime = java.lang.System.currentTimeMillis();
fs.write(buffer, 0, byteRead); //读取
}
threadId && threadId.isAlive() && threadId.interrupt();
toastLog('下载完成');
}