Discuz!NT|BBS|论坛

注册

 

发新话题 回复该主题

flutter 程序内下载安装更新 [复制链接]

1#

FlutterDownloader.initialize(    debug: true
);







import 'dart:isolate';import 'dart:ui';
import 'dart:async';
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';
import 'package:package_info/package_info.dart';
import 'package:path_provider/path_provider.dart';
import 'packagepen_file/open_file.dart';
import 'packagelutter_downloader/flutter_downloader.dart';
import 'package:progress_dialog/progress_dialog.dart';







String serviceVersionCode = '';  String ANDROID_FILE_PATH = '';
  String ANDROID_UPDATE_MODE = '0';
  String IOS_URL = '';
  ProgressDialog pr;
  String apkName ='toppmskhapp.apk';
  String appPath = '';
  ReceivePort _port = ReceivePort();




  @override
void initState() {
  super.initState();

  eventBus.on<UserLoggedInEvent>().listen((event){
    //print("homepage event bus..." + event.text);

});

  IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
  _port.listen(_updateDownLoadInfo);
  FlutterDownloader.registerCallback(_downLoadCallback);

  // 如果是android则执更新

if(Platform.isAndroid){

    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      var currentVersionCode = packageInfo.version;
      _getNewVersionAPP(context, currentVersionCode);
    });


  }

}


/// 执行更新操作
  _doUpdate(BuildContext context) async {
    Navigator.pop(context);
    _executeDownload(context);

  }

  /// 下载最新apk包
  Future<void> _executeDownload(BuildContext context) async {
    pr = new ProgressDialog(
      context,
      type: ProgressDialogType.Download,
      isDismissible: true,
      showLogs: true,
    );
    pr.style(message: '准备下载...');
    if (!pr.isShowing()) {
      pr.show();
    }

    final path = await _apkLocalPath;
    await FlutterDownloader.enqueue(
        url: ANDROID_FILE_PATH,
        savedDir: path,
        fileName: apkName,
        showNotification: true,
        openFileFromNotification: true
    );
  }

  /// 下载进度回调函数
  static void _downLoadCallback(String id, DownloadTaskStatus status, int progress) {
    final SendPort send = IsolateNameServer.lookupPortByName('downloader_send_port');
    send.send([id, status, progress]);
  }

  /// 更新下载进度框
  _updateDownLoadInfo(dynamic data) {
    DownloadTaskStatus status = data[1];
    int progress = data[2];
    if (status == DownloadTaskStatus.running) {
      pr.update(progress: double.parse(progress.toString()), message: "下载中,请稍后…");
    }
    if (status == DownloadTaskStatus.failed) {
      if (pr.isShowing()) {
        pr.hide();
      }
    }

    if (status == DownloadTaskStatus.complete) {
      if (pr.isShowing()) {
        pr.hide();
      }
      _installApk();
    }
  }

  /// 安装apk
  Future<Null> _installApk() async {
    await OpenFile.open(appPath + '/' + apkName);
  }

  /// 获取apk存储位置
  Future<String> get _apkLocalPath async {
    final directory = await getExternalStorageDirectory();
    String path = directory.path  + Platform.pathSeparator + 'Download';;
    final savedDir = Directory(path);
    bool hasExisted = await savedDir.exists();
    if (!hasExisted) {
      await savedDir.create();
    }
    this.setState((){
      appPath = path;
    });
    return path;
  }

  Future<bool> checkPermission() async {
    //检查是否已有读写内存权限
    PermissionStatus status = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
    print(status);

    //判断如果还没拥有读写权限就申请获取权限
    if(status != PermissionStatus.granted){
      var map = await PermissionHandler().requestPermissions([PermissionGroup.storage]);
      if(map[PermissionGroup.storage] != PermissionStatus.granted){
        return false;
      }
    }
    return true;
  }
分享 转发
TOP
发新话题 回复该主题