通过编程Xamarin.Forms(Android)安装APK



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

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

使用DependencyServices

应用程序.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。支持它不再支持了。

我也试过这个:

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。在API第29级中,ActionView和ACTION_INSTALL_PACKAGE已被弃用。

你尝试过这种解决方案吗?Android PackageInstaller未安装APK

最新更新