tnblog
首页
视频
资源
登录

uniapp 应用热更新,进度条/权限

149人阅读 2024/12/13 17:30 总访问:278338 评论:0 收藏:0 手机
分类: 前端

/**

* 检查安装应用权限

*/

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

             }

    

         }

         }

     })

},


评价

uniapp</font> APP开发基础

前言uniapp是基于vue.js实现的,代码实现方法不能用jQuery的思路去解决问题,基本使用template: 模板view: 视图,使用时把v...

uniapp</font>页面跳转传递参数过长

前言页面传递参数过长如果需要传递的参数content过长,传递的时候会有问题所以使用encodeURIComponent来传递uni.navigateTo...

uniapp</font> webview跳转到app内部页面

官方文档:https://ask.dcloud.net.cn/article/35083相关问题:https://ask.dcloud.net.cn/question/93777引入需要的Js:&lt...

uniapp</font>中webview跳转到app内部页面

官方文档:https://ask.dcloud.net.cn/article/35083第一步:在webview加载的页面引入Js&lt;!--uni的SDK,必须引用。--&gt;...

uniapp</font>返回上一级页面刷新页面

当前页面: //uniapp返回上一级页面刷新页面 let pages = getCurrentPages(); // 当前页面 let beforePage ...

uniapp</font> uni.navigateBack无法返回

注意测试的时候,不要直接打开当前页进行测试。要从其他页面点进去在返回测试。不然你获取上一页是没有的,就会报错。

uniapp</font>确认提示框

代码如下: uni.showModal({ title: &#39;提示&#39;, content: &#39;这是一个模态弹窗&#39;, success: fun...

uniapp</font>预览图片详解

一、预览图片的基本用法在uniapp中,我们可以使用uni.previewImage()API对图片进行预览,具体使用方法如下: uni.previewI...

uniapp</font>文档在线浏览,</font>pdfexcelword等

示例代码uni.downloadFile({ url: &#39;https://example.com/somefile.pdf&#39;, success: function (res) { va...

uniapp</font> 微信小程序 上传图片,</font>上传文件,</font>上传视频等,</font>上传组件封装

上传图片文件还可以参考:https://www.tnblog.net/aojiancc2/article/details/8233 这个里边有关于.net6实现图片上传后端的...

uniapp</font> vue3 引用样式表样式

这样即可: &lt;style scoped lang=&quot;scss&quot;&gt; @import &#39;@/static/css/report-box.scss&#39;; &lt;/style...

uniapp</font> 离线打包配置

下载Android Studio 并配置好JavaSDK查看HBuilderX版本然后去下载对应的SDK版本https://pan.baidu.com/s/1ZYxRVH23EbldPQqO...

uniapp</font> 离线打包配置高德地图

没有申请key的去高德开放平台申请的Android平台Keyhttps://console.amap.com/dev/key/app SHA1 在前面申请云端证书里面找 ...

uniapp</font> plus.runtime.install 安装失败

APP 权限配置添加&quot;&lt;uses-permission android:name=\&quot;android.permission.REQUEST_INSTALL_PACKAGES\&quot;/&g...

css弹性盒子,</font>flex布局

css弹性盒子由于版本不同浏览器问题造成了一些不同的写法display:flexbox;在google浏览器中如果使用下面的写法就不行displa...
若人生只如初见,愿还是相遇的那个下午,阳光明媚转身一笑便如春暖花开
排名
16
文章
53
粉丝
7
评论
13
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术