【问题标题】:Install APK by programmatically Xamarin.Forms (Android)通过编程方式安装 APK Xamarin.Forms (Android)
【发布时间】:2022-05-05 03:08:29
【问题描述】:

到目前为止,我的代码如下所示:

1.下载 APK 文件并将其保存到内部存储中:

使用依赖服务

App.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");
        }
    }

Android : 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 文件:

App.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 的其他方法吗?

【问题讨论】:

  • 你检查过这个线程吗:stackoverflow.com/questions/64283181/…?不要忘记在 manifest 中更改对 androidx 提供程序的支持。
  • 我已经检查过了。它与我的问题相同,不支持“Android.Support.V4.Content”
  • 两个音符。 1 - 除了所有者之外的任何其他应用程序都无法访问保存到应用程序内部文件夹的任何内容,不要保存到内部存储,使用下载目录。 2 - 尝试使用 Xamarin.Essentials 中的 Launcher 将安装重定向到系统 docs.microsoft.com/en-us/xamarin/essentials/…
  • 我一直在尝试您的建议,但这句话不起作用:string pathToNewFile = Path.Combine(Android.OS.Environment.DirectoryDownloads,"nucleo.apk"); + webClient.DownloadFileAsync(new Uri(url),pathToNewFile);
  • not work 是什么意思?下载失败还是抛出异常?

标签: c# android .net xamarin


【解决方案1】:

请改用 PackageInstaller。 ActionView 和 ACTION_INSTALL_PACKAGE 在 API 级别 29 中已被弃用。

您是否尝试过此解决方案? Android PackageInstaller not installing APK

【讨论】:

  • 嗨!如果您认为该问题已被回答,请将问题标记为重复,避免在答案区域中发布指向其他 SO 问题的链接。
猜你喜欢
  • 2011-06-13
  • 1970-01-01
  • 2019-06-06
  • 2011-06-25
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多