c# - 以编程方式安装 APK Xamarin.Forms (Android)

标签 c# android .net xamarin

到目前为止,我的代码如下所示:
1.下载APK文件并保存到内部存储:
使用依赖服务
应用程序.xaml.cs

    IDownloader downloader = DependencyService.Get<IDownloader>();

    protected override void OnStart(){
        downloader.OnFileDownloaded+=OnFileDownloaded;
        downloader.DownloadFile("http://localhost:8080/download","folder"); 
    }

    private void OnFileDownloaded(object sender,DownloadEventArgs e) {
        if(e.FileSaved) {
            App.Current.MainPage.DisplayAlert("XF Downloader","File Saved Successfully","Close"); 
        } else {
            App.Current.MainPage.DisplayAlert("XF Downloader","Error while saving the file","Close");
        }
    }
安卓:AndroidDownloader.cs
[assembly: Dependency(typeof(NoguianaNucleo.Droid.AndroidDownloader))]
namespace NoguianaNucleo.Droid { 
  public class AndroidDownloader: IDownloader {
    public event EventHandler<DownloadEventArgs> OnFileDownloaded;

    public void DownloadFile(string url,string folder) {

        string pathToNewFolder = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),folder);
        Directory.CreateDirectory(pathToNewFolder);

        try {
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted+=new AsyncCompletedEventHandler(Completed);
            string pathToNewFile = Path.Combine(pathToNewFolder,"nucleo.apk");
            webClient.DownloadFileAsync(new Uri(url),pathToNewFile);
        } catch(Exception ex) {
            if(OnFileDownloaded!=null)
                OnFileDownloaded.Invoke(this,new DownloadEventArgs(false));
        }
    }

    private void Completed(object sender,AsyncCompletedEventArgs e) {
        if(e.Error!=null) {
            App.Current.MainPage.DisplayAlert("Error", e.Error.Message,"Ok");
            if(OnFileDownloaded!=null)
                OnFileDownloaded.Invoke(this,new DownloadEventArgs(false));
        } else {
            if(OnFileDownloaded!=null)
                OnFileDownloaded.Invoke(this,new DownloadEventArgs(true));
        }
    }
  }
}
2.从内部存储安装APK文件:
应用程序.xaml.cs
    public void OpenApk(string filepath) {
        Java.IO.File file = new Java.IO.File(filepath);
        Intent install = new Intent(Intent.ActionView);

        // Old Approach
        if(Android.OS.Build.VERSION.SdkInt<Android.OS.BuildVersionCodes.N) {
            install.SetFlags(ActivityFlags.NewTask|ActivityFlags.GrantReadUriPermission);
            install.SetDataAndType(Android.Net.Uri.FromFile(file),"application/vnd.android.package-archive"); //mimeType
        } else {
            Android.Net.Uri apkURI = Android.Support.V4.Content.FileProvider.GetUriForFile(Android.App.Application.Context,Android.App.Application.Context.ApplicationContext.PackageName+".fileprovider",file);
            install.SetDataAndType(apkURI,"application/vnd.android.package-archive");
            install.AddFlags(ActivityFlags.NewTask);
            install.AddFlags(ActivityFlags.GrantReadUriPermission);
        }

        Android.App.Application.Context.StartActivity(install);
    }
最后一个功能不起作用。我认为 Android.Support 它不再支持了。
我也试过这个:
   var downloadUri = Android.Net.Uri.Parse("/data/user/0/noguiana.nucleo/files/noguiana/nucleo.apk");
   Intent install = new Intent(Intent.ActionInstallPackage);
   install.AddFlags(ActivityFlags.GrantReadUriPermission);
   install.AddFlags(ActivityFlags.GrantWriteUriPermission);
   install.AddFlags(ActivityFlags.GrantPersistableUriPermission);
   install.SetDataAndType(downloadUri,"application/vnd.android.package-archive");     
   context.StartActivity(install);
没有任何效果
你知道在 Xamarin.Forms (Android) 中以编程方式安装 APK 的其他方法吗?

最佳答案

请改用 PackageInstaller。
ActionView 和 ACTION_INSTALL_PACKAGE 在 API 级别 29 中已被弃用。
你试过这个解决方案吗?
Android PackageInstaller not installing APK

关于c# - 以编程方式安装 APK Xamarin.Forms (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70212833/

相关文章:

c# - 如何检查 bool 列表中的值?

c# - 使用 C# 将文件/文件夹添加到 Program Files x86 文件夹

android - iText 库 - 不显示西里尔文(俄语)符号

java - 使用 Gson 解析带有嵌套数组的 json

c# - 关于两种不同类扩展模式的问题

javascript - 每天用另一张图片替换一张图片 (.jpg) 的脚本?

.net - 2010 年对 .NET Compact Framework 的更新?

c# - 制作一个完全透明的矩形(窗口中的一个洞)WPF

c# - 将图像与文本一起添加到 ListView (Metro App)

android - YouTubePlayerView被com.lbe.security.service.core.client.a.f遮盖了