/**
* 检查安装应用权限
*/
checkInstallPermission() {
return new Promise((resolve, reject) => {
try {
const Build = plus.android.importClass('android.os.Build');
// Android 8.0以下无需检查此权限
if (Build.VERSION.SDK_INT < 26) {
resolve(true);
return;
}
const main = plus.android.runtimeMainActivity();
const PackageManager = plus.android.importClass('android.content.pm.PackageManager');
const packageManager = main.getPackageManager();
if (packageManager.canRequestPackageInstalls()) {
// 已经获取权限
resolve(true);
} else {
// 没有权限,弹窗询问是否去设置
uni.showModal({
title: '提示',
content: '安装应用需要打开未知来源权限,是否去设置?',
success: (res) => {
if (res.confirm) {
// 跳转到设置页面
const Intent = plus.android.importClass(
'android.content.Intent');
const Settings = plus.android.importClass(
'android.provider.Settings');
const Uri = plus.android.importClass('android.net.Uri');
const intent = new Intent(Settings
.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
intent.setData(Uri.parse('package:' + main.getPackageName()));
// 监听设置结果
main.onActivityResult = (requestCode, resultCode, data) => {
if (requestCode === 10086) {
if (packageManager.canRequestPackageInstalls()) {
resolve(true);
} else {
reject('用户未授权');
}
}
};
// 跳转设置页面
main.startActivityForResult(intent, 10086);
} else {
reject('用户取消授权');
}
},
fail: () => {
reject('弹窗失败');
}
});
}
} catch (e) {
console.error('检查安装权限失败:', e);
reject(e);
}
});
},
async upversion(version_file) {
const _this = this;
try {
// 检查权限
await this.checkInstallPermission();
_this.install(version_file)
} catch (error) {
console.log('权限检查失败:', error);
uni.showToast({
title: '无法安装应用',
icon: 'none'
});
}
},
install(version_file) {
const _this = this;
uni.showModal({
title: "发现新版本",
content: "确认下载更新",
showCancel: false,
success: (res) => {
if (res.confirm == true) {
console.log(version_file);
var dtask = plus.downloader.createDownload(
version_file, {},
function(d, status) {
if (status == 200) {
console.log(JSON.stringify(d.filename))
const realPath = plus.io.convertLocalFileSystemURL(d.filename);
console.log('转换后路径:', realPath);
plus.runtime.install(realPath, {
force: true
}, () => {
console.log('安装成功');
}, (e) => {
console.error('安装失败:', e);
_this.updata = false
uni.showToast({
title: '安装失败',
icon: 'none',
duration: 1500
});
});
} else {
uni.showToast({
title: '更新失败',
duration: 1500
});
_this.updata = false
}
});
try {
dtask.start(); // 开启下载的任务
var prg = 0;
var showLoading = plus.nativeUI.showWaiting(
"正在下载"); //创建一个showWaiting对象
dtask.addEventListener('statechanged', function(
task,
status
) {
// 给下载任务设置一个监听 并根据状态 做操作
switch (task.state) {
case 1:
showLoading.setTitle("正在下载");
_this.updata = true
break;
case 2:
showLoading.setTitle("已连接到服务器");
break;
case 3:
prg = parseInt(
(parseFloat(task
.downloadedSize) /
parseFloat(task
.totalSize)
) *
100
);
showLoading.setTitle(" 正在下载" +
prg +
"% ");
break;
case 4:
plus.nativeUI.closeWaiting();
//下载完成
break;
}
});
} catch (err) {
plus.nativeUI.closeWaiting();
uni.showToast({
title: '更新失败-03',
mask: false,
duration: 1500
});
_this.updata = false
}
}
}
})
},