void checkUpdate_DoSomething(bool isDownloadFromApplication) { string version = Xamarin.Essentials.VersionTracking.CurrentVersion; string build = Xamarin.Essentials.VersionTracking.CurrentBuild; string platform = Xamarin.Essentials.DeviceInfo.Platform.ToString(); UpdateInfo argUpdateInfo = new UpdateInfo() { Platform = platform, Version = int.Parse(build) }; Acr.UserDialogs.UserDialogs.Instance.ShowLoading("访问服务器中,请稍候..."); new WebService().GetLastestVersion ( updateInfoJsonStr: Util.JsonUtils.SerializeObject(argUpdateInfo), page: mPage, handle: async(soapResult) => { Acr.UserDialogs.UserDialogs.Instance.HideLoading(); if (soapResult.IsComplete == false) { await mPage.DisplayAlert("Error", soapResult.ExceptionInfo, "确定"); return; } else if (soapResult.IsSuccess == false) { await mPage.DisplayAlert("Error", soapResult.BusinessExceptionInfo, "确定"); return; } else { UpdateInfo updateInfo = Util.JsonUtils.DeserializeObject <UpdateInfo>(soapResult.ReturnObjectJson); if (updateInfo.IsLastestVersion == true) { Acr.UserDialogs.UserDialogs.Instance.Toast(updateInfo.Message); return; } // 询问用户是否更新 bool r1 = await mPage.DisplayAlert ( title: "确认更新", message: updateInfo.Message, accept: "更新", cancel: "拒绝" ); if (r1 == false) { return; } // 用户拒绝更新 if (isDownloadFromApplication == true) { download(updateInfo); } else { browserOpenUrl(updateInfo); } } } ); }
void download(UpdateInfo updateInfo) { System.ComponentModel.BackgroundWorker bgWorker = null; Acr.UserDialogs.IProgressDialog progressDialog = null; bgWorker = new System.ComponentModel.BackgroundWorker(); bgWorker.DoWork += (bgSender, bgArgs) => { var objArgs = bgArgs.Argument as object[]; UpdateInfo _updateInfo_ = objArgs[0] as UpdateInfo; new Util.DownloadUtils().DownloadFileByHttpRequest ( requestUri: _updateInfo_.Url, fileLength: _updateInfo_.FileLength, saveFileFolder: System.IO.Path.Combine(Common.StaticInfo.AndroidExternalCachePath, "UpdateAPKs"), renameDownloadFileName: string.Empty, // string.Empty 取默认文件名 backgroundWorker: bgWorker, eventArgs: bgArgs // 传入 bgArgs 参数, 在 DownloadFileByHttpRequest 返回结果 -- 下载文件的最终路径 ); System.Threading.Thread.Sleep(500); // 为显示进度 100 % }; bgWorker.RunWorkerCompleted += (bgSender, bgResult) => { progressDialog.Hide(); if (bgResult.Error != null) { mPage.DisplayAlert ( title: "捕获异常", message: bgResult.Error.GetFullInfo(), cancel: "确认" ); } else { try { string path = bgResult.Result as string; System.Diagnostics.Debug.WriteLine($"打开APK安装器。APK路径[{path}]"); App.AndroidIntentUtils.InstallAPK(path); } catch (Exception ex) { string msg = $"{ex.GetFullInfo()}"; System.Diagnostics.Debug.WriteLine(msg); mPage.DisplayAlert ( title: "捕获异常", message: msg, cancel: "确认" ); } } }; bgWorker.WorkerReportsProgress = true; bgWorker.ProgressChanged += (bgSender, bgArgs) => { if (progressDialog.IsShowing == false) { progressDialog.Show(); } progressDialog.Title = $"正在下载..."; progressDialog.PercentComplete = bgArgs.ProgressPercentage; }; progressDialog = Acr.UserDialogs.UserDialogs.Instance.Progress(); bgWorker.RunWorkerAsync(new object[] { updateInfo }); }