安卓app应用内升级demo
1、使用 package_info 获取本地版本信息,然后与线上版本对比,看是否需要更新
1 2 3 4 5
| import 'package:package_info/package_info.dart';
PackageInfo packageInfo = await PackageInfo.fromPlatform(); String version = packageInfo.version;
|
2、使用 permission_handler 来获取读写权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import 'package:permission_handler/permission_handler.dart';
checkPermission() async { PermissionStatus status = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
if(status != PermissionStatus.granted){ var map = await PermissionHandler().requestPermissions([PermissionGroup.storage]); if(map[PermissionGroup.stoßrage] != PermissionStatus.granted){ return false; } } }
|
3、使用 flutter_downloader 下载最新版本的安装包
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
| import 'package:flutter_downloader/flutter_downloader.dart';
Future<String> executeDownload(String url) async { final path = await BackUpdate()._apkLocalPath();
final taskId = await FlutterDownloader.enqueue( url: url, fileName: 'update.apk', savedDir: path, showNotification: false, openFileFromNotification: false);
FlutterDownloader.registerCallback((id, status, progress) { setState(() => this.progress = progress);
if (taskId == id && status == DownloadTaskStatus.complete) { Navigator.of(context).pop(); BackUpdate()._installApk(); } });
return taskId; }
|
4、使用 install_plugin 打开下载好的安装包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import 'package:install_plugin/install_plugin.dart';
Future<Null> _installApk() async { try { final path = await _apkLocalPath();
InstallPlugin.installApk(path + '/update.apk', 'com.example.app_update_demo') .then((result) { print('install apk $result'); }).catchError((error) { print('install apk error: $error'); }); } on PlatformException catch (_) {} }
|
完整代码: 安卓app应用内升级demo
本文链接:
https://www.hehuapei.com/flutter-andriod-app-update/index.html