【MAUI】自动更新功能的安卓App
创始人
2024-05-12 05:54:00

自动更新功能的安卓App

  • 自动更新主要下面4个步骤
    • 更新服务
    • 测试页面:MainPage.xaml
    • 测试

自动更新主要下面4个步骤

1、获取最新版本号
2、提示用户发现更新,等待用户确认更新
3、下载最新的apk包
4、安装apk包

更新服务

为简单示例:直接在android平台文件夹下新建静态类:UpgradeService.cs
在这里插入图片描述
包含:

  • 检查更新
  • 下载文件
  • 安装apk包
    因为存在命名空间冲突问题,故需改下命名空间。
       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的文件名称。

测试页面:MainPage.xaml

        

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
并得到老师的指导,感谢!

相关内容

热门资讯

demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...