manifest.json-APP权限设置需勾选:
“<uses-permission android:name=“android.permission.INSTALL_PACKAGES”/>”,
“<uses-permission android:name=“android.permission.REQUEST_INSTALL_PACKAGES”/>”
直接copy就能用,改个链接地址就行
const downloadTask = plus.downloader.createDownload(
"https://led.szhpzy.com/file/assets/abc.apk",
{
method: "GET",
},
(task, status) => {
console.log(status, "status");
if (status == 200) {
//下载成功
console.log(task.filename);
plus.runtime.install(
task.filename,
{
force: true,
},
function () {
console.log(888);
// 安装成功,可以重启应用
plus.runtime.restart();
},
function (e) {
// 安装失败,处理错误
console.error("安装失败: " + e.message);
}
);
} else {
uni.showToast({
title: "下载失败",
icon: "error",
});
}
}
);
//监听下载过程
downloadTask.addEventListener("statechanged", (task, status) => {
switch (task.state) {
case 1: // 开始
console.log("开始");
break;
case 2: //已连接到服务器
console.log("已连接到服务器");
break;
case 3: // 已接收到数据
console.log("已接收到数据 ");
let hasProgress = task.totalSize && task.totalSize > 0; //是否能获取到App大小
if (hasProgress) {
let current = parseInt(
(100 * task.downloadedSize) / task.totalSize
); //获取下载进度百分比
console.log(current);
}
break;
case 4: // 下载完成
console.log("下载完成");
break;
}
});
//开始执行下载
downloadTask.start();