1、获取最新版本号
2、提示用户发现更新,等待用户确认更新
3、下载最新的apk包
4、安装apk包
为简单示例:直接在android平台文件夹下新建静态类:UpgradeService.cs
包含:
public static class UpgradeService {readonly static HttpClient _client;static UpgradeService(){_client = new HttpClient();}public static async Task> CheckUpdatesAsync(string url){var result = new Dictionary();// 获取当前版本号var currentVersion = VersionTracking.CurrentVersion;var latestVersion = await _client.GetStringAsync(url);result.Add("CurrentVersion", currentVersion);result.Add("LatestVersion", latestVersion);return result;}public static void InstallNewVersion(){var file = $"{FileSystem.AppDataDirectory}/{"com.shez.addressbook-Signed.apk"}";var apkFile = new Java.IO.File(file);var intent = new Intent(Intent.ActionView);// 判断Android版本if (Build.VERSION.SdkInt >= BuildVersionCodes.N){//给临时读取权限intent.SetFlags(ActivityFlags.GrantReadUriPermission);var uri = FileProvider.GetUriForFile(Android.App.Application.Context, "com.shez.addressbook.fileprovider", apkFile);// 设置显式 MIME 数据类型intent.SetDataAndType(uri, "application/vnd.android.package-archive");}else{intent.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(file)), "application/vnd.android.package-archive");}//指定以新任务的方式启动Activityintent.AddFlags(ActivityFlags.NewTask);//激活一个新的ActivityAndroid.App.Application.Context.StartActivity(intent);} public static async Task DownloadFileAsync(string url, Action action){var req = new HttpRequestMessage(new HttpMethod("GET"), url);var response = _client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead).Result;var allLength = response.Content.Headers.ContentLength;var stream = await response.Content.ReadAsStreamAsync();var file = $"{FileSystem.AppDataDirectory}/{"com.shez.addressbook-Signed.apk"}";await using var fileStream = new FileStream(file, FileMode.Create);fileStream.Flush();await using (stream){var buffer = new byte[10240];var readLength = 0;int length;while ((length = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0){readLength += length;action(readLength, allLength!.Value);// 写入到文件fileStream.Write(buffer, 0, length);}}}}
这里需要使用到 FileProvider,在Android 7之后出于安全考虑不再支持content://URL 或file:///URL这种文件访问方式,可参考FileProvider | Android Developers,我们先添加一下对应配置
在Platforms/Android/Resources下面新建xml文件夹,并添加provider_paths.xml文件
修改Platforms / Android下面的AndroidManifest.xml文件,在application下添加provider,再添加一个安卓安装的权限“REQUEST_INSTALL_PACKAGES”
通过WebApi请求获取发布的最新版本号。为简单示例,API读取配置文件获取。
其中com.shez.addressbook-Signed.apk为apk的文件名称。
cs代码:
为简单测试版本,Webapi接口直接读取配置文件版本。
直接文本显示百分比和下载字节数。
public MainPage(){InitializeComponent();lblCurrentVersion.Text ="当前版本:"+ VersionTracking.CurrentVersion;}private async void OnReLoginClicked(object sender, EventArgs e){await Shell.Current.GoToAsync("//LoginPage");}private async void OnExitClicked(object sender, EventArgs e){Environment.Exit(0);}/// /// 更新/// /// /// private async void OnUpdateClicked(object sender, EventArgs e){var result = await UpgradeService.CheckUpdatesAsync($"{ Operator.BaseUrl}/api/File/AddressBookVer");if (result["CurrentVersion"] != result["LatestVersion"]){var confirm = await Shell.Current.DisplayAlert("问题?", $"检测到新版本,是否升级,版本号为:" + result["LatestVersion"], "确定", "取消");if (confirm){await UpgradeService.DownloadFileAsync($"{Operator.BaseUrl}/api/File/AddressBook", DownloadProgressChanged);//https://你的域名/com.masa.mauidemo.apkUpgradeService.InstallNewVersion();UpdateBtn.IsEnabled = true;}}else{await Shell.Current.DisplayAlert("提示",$"当前版本已经是最新版,版本号为:" + result["LatestVersion"],"确定");}}private void DownloadProgressChanged(long readLength, long allLength){UpdateBtn.IsEnabled = false; var c = (int)(readLength * 100 / allLength);if (c > 0 && c % 5 == 0) //刷新进度为每5%更新一次,过快的刷新会导致页面显示数值与实际不一致{var BytesReceived = readLength / 1024; //当前已经下载的Kbvar TotalBytesToReceive = allLength / 1024; //文件总大小KblblPross.Text =$"正在下载中{c}% {BytesReceived}/{TotalBytesToReceive}...";if (c == 100){UpdateBtn.IsEnabled = true;}}}



参考:https://blog.csdn.net/sunday866/article/details/126405322
并得到老师的指导,感谢!