Maui自动更新(安卓)

Maui自动更新(安卓)

  1. 在服务器创建版本查询txt或者接口等其他方式。

    1
    2
    
    HttpClient httpClient = new();
    var response = await httpClient.GetStringAsync("http://x.x.x.x/version.txt");
    
  2. 查询本地程序版本,对比版本号是否需要更新。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    // 简单信息
    string name = AppInfo.Current.Name;
    string package = AppInfo.Current.PackageName;
    string version = AppInfo.Current.VersionString;
    string build = AppInfo.Current.BuildString;
    // 详细信息
    var Text = VersionTracking.Default.IsFirstLaunchEver.ToString();
    Text = VersionTracking.Default.IsFirstLaunchForCurrentVersion.ToString();
    Text = VersionTracking.Default.IsFirstLaunchForCurrentBuild.ToString();
    Text = VersionTracking.Default.CurrentVersion.ToString();
    Text = VersionTracking.Default.CurrentBuild.ToString();
    Text = VersionTracking.Default.FirstInstalledVersion.ToString();
    Text = VersionTracking.Default.FirstInstalledBuild.ToString();
    Text = String.Join(',', VersionTracking.Default.VersionHistory);
    Text = String.Join(',', VersionTracking.Default.BuildHistory);
    
    // These two properties may be null if this is the first version
    Text = VersionTracking.Default.PreviousVersion?.ToString() ?? "none";
    Text = VersionTracking.Default.PreviousBuild?.ToString() ?? "none";
    
  3. 下载新版程序(C#HttpClient下载进度),保存到目录Android.App.Application.Context.GetExternalFilesDir(null)FileSystem.Current.CacheDirectory或者Android.App.Application.Context.GetExternalCacheDirs().First().AbsolutePath

  4. 启动安装程序。
    添加权限设置到AndroidManifest.xml文件的applicationttag下。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>
    

    再添加包相关权限。

    1
    2
    3
    4
    
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
    <uses-permission android:name="android.permission.DELETE_PACKAGES" />
    

    \Platforms\Android\Resources创建xml文件夹\Platforms\Android\Resources\xml,在文件夹下面创建provider_paths.xml,设置文件属性中的生成操作/Build ActionAndroidResourceprovider_paths.xml文件内容如下。

    1
    2
    3
    4
    5
    6
    7
    
    <?xml version="1.0" encoding="utf-8" ?> 
    <paths xmlns:android="schemas.android.com/apk/res/android"> 
        <external-path name="external_files" path="." />
        <external-files-path name="external_files_path" path="." />
        <files-path name="internal_files_path" path="." />
        <cache-path name="internal_cache_path" path="." />
    </paths>
    

    安装代码如下。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    async Task InstallAPK()
    {
    #if ANDROID
                var context = Android.App.Application.Context;
                var path = Path.Combine(Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath, "com.companyname.mauiapp.apk"); // file文件夹
                path = Path.Combine(Android.App.Application.Context.GetExternalCacheDirs().First().AbsolutePath, "com.companyname.mauiapp.apk"); // cache文件夹
                Java.IO.File file = new Java.IO.File(path);
    
                try
                {
                    using (Android.Content.Intent install = new Android.Content.Intent(Android.Content.Intent.ActionView))
                    {
                        Android.Net.Uri apkURI = AndroidX.Core.Content.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);
                        apkURI = Microsoft.Maui.Storage.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file); // 使用maui库
                        install.SetDataAndType(apkURI, "application/vnd.android.package-archive");
                        install.AddFlags(Android.Content.ActivityFlags.NewTask);
                        install.AddFlags(Android.Content.ActivityFlags.GrantReadUriPermission);
                        install.AddFlags(Android.Content.ActivityFlags.ClearTop);
                        install.PutExtra(Android.Content.Intent.ExtraNotUnknownSource, true);
    
                        Platform.CurrentActivity.StartActivity(install);
                    }
                }
                catch (Exception ex)
                {
                }
    #endif
    }
    

    如果使用Platform.CurrentActivity.StartActivityForResult(install);方法执行安装,可以在MainActivity中重写OnActivityResult方法。

    1
    2
    3
    4
    
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
    {
         base.OnActivityResult(requestCode, resultCode, data);
    }
    
  5. 清理安装包Maui接收包安装删除广播(安卓)

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy