当前位置:   article > 正文

reactnative实现app应用内更新_react-native android实现内更新

react-native android实现内更新

reactnative实现app应用内更新

最近用reactnative写app,需要有app升级功能。查看资料,很多都是热更新,需要在CodePush等框架注册账号,然后传包到他们系统,下载次数有限制。并不符合要求。
于是使用rn-fetch-blob下载安装包在运行替换。

安装 rn-fetch-blob

npm install rn-fetch-blob
  • 1

or

yarn add rn-fetch-blob
  • 1

安装 react-native-device-info

安装react-native-device-info获取当前版本号

npm install react-native-device-info
  • 1

or

yarn add react-native-device-info
  • 1

使用

import RNFetchBlob from 'rn-fetch-blob';
import DeviceInfo from 'react-native-device-info';

const Update = () => {
if(DeviceInfo.getVersion !== newVersion){
  const android = RNFetchBlob.android;
  //配置手机系统通知栏下载文件通知,下载成功后点击通知可运行apk文件
  RNFetchBlob.config({
    addAndroidDownloads: {
      useDownloadManager: true,
      title: '更新app',
      description: 'An APK that will be installed',
      mime: 'application/vnd.android.package-archive',
      path: `${RNFetchBlob.fs.dirs.DownloadDir}/update.apk`,
      mediaScannable: true,
      notification: true
    }
  }).fetch(
    'GET',
     url, //apk下载地址
  ).progress({ interval: 250 }, (received, total) => {
    setpercent(received / total)// 下载进度
}).then(res => {
  setpercent(100)
    //下载成功后自动打开安装已下载apk文件
    console.log('要运行咯。。。。。');
    android.actionViewIntent(
      res.path(),
      'application/vnd.android.package-archive'
    );
  }).catch((err) => {
    console.log('err',err);
    
  })
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/938442
推荐阅读
相关标签
  

闽ICP备14008679号