一、权限
manifest.json-APP权限设置需勾选:
“<uses-permission android:name=“android.permission.INSTALL_PACKAGES”/>”,
“<uses-permission android:name=“android.permission.REQUEST_INSTALL_PACKAGES”/>”
二、下载与安装
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();