downloadAndInstallApk method

Future<bool> downloadAndInstallApk({
  1. required String fileUrl,
  2. required String fileDirectory,
  3. required String fileName,
  4. bool isDeleteOriginalFile = true,
  5. String? explainContent,
  6. String? positiveText,
  7. String? negativeText,
  8. dynamic onDownloadingListener(
    1. double progress
    )?,
  9. dynamic onCancelTagListener(
    1. String cancelTag
    )?,
  10. OnStateChangeListener? onStateListener,
})

下载APK到沙盒目录下,并执行安装操作

仅支持Android

Implementation

Future<bool> downloadAndInstallApk({
  required String fileUrl,
  required String fileDirectory,
  required String fileName,
  bool isDeleteOriginalFile = true,
  String? explainContent,
  String? positiveText,
  String? negativeText,
  Function(double progress)? onDownloadingListener,
  Function(String cancelTag)? onCancelTagListener,
  OnStateChangeListener? onStateListener,
}) async {
  if (defaultTargetPlatform != TargetPlatform.android) {
    return false;
  }
  if (fileUrl.isEmpty || fileDirectory.isEmpty || fileName.isEmpty) {
    debugPrint(
        "EasyAppInstaller.downloadAndInstallApk: fileUrl/fileDirectory/fileName must not be empty!");
    return false;
  }
  final arguments = <String, dynamic>{
    "fileUrl": fileUrl,
    "fileDirectory": fileDirectory,
    "fileName": fileName,
    "isDeleteOriginalFile": isDeleteOriginalFile,
    "explainContent": explainContent,
    "positiveText": positiveText,
    "negativeText": negativeText,
  };

  if (onDownloadingListener != null ||
      onCancelTagListener != null ||
      onStateListener != null) {
    _channel.setMethodCallHandler((call) async {
      switch (call.method) {
        case EasyAppInstallerConstant.methodDownloadProgress:
          if (call.arguments is double && onDownloadingListener != null) {
            onDownloadingListener((call.arguments as double));
          }
          break;
        case EasyAppInstallerConstant.methodCancelTag:
          if (call.arguments is String && onCancelTagListener != null) {
            onCancelTagListener((call.arguments as String));
          }
          break;
        case EasyAppInstallerConstant.methodDownloadState:
          if (onStateListener != null && call.arguments != null) {
            _handleDownloadState(call.arguments, onStateListener);
          }
          break;
      }
    });
  }

  try {
    await _channel.invokeMethod("downloadAndInstallApk", arguments);
    return true;
  } catch (e) {
    debugPrint("EasyAppInstaller.downloadAndInstallApk: $e");
    return false;
  }
}