本文介绍了Xamarin Android可编程下载安装APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此我有一个用于验证更新的按钮,当我单击它时,我希望它下载并安装APK,但我一直收到错误There was a problem parsing the package
。
下载->;
var imagePath = Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString(), "abcFichaObrasUpdate");
using (WebClient client = new WebClient())
{
try
{
if (!Directory.Exists(imagePath.ToString()))
{
Directory.CreateDirectory(imagePath.ToString());
}
Uri uri = new Uri(remoteUri);
client.DownloadFileCompleted += (s, e) => Extract(s, e, progress);
client.DownloadProgressChanged += (s, e) => ProgessChanged(s, e);
client.DownloadFileAsync(uri, imagePath + "/" + "update.apk");
}
catch (Exception)
{
throw;
}
}
因此,下载完成后,它将保存在类似/storage/emulated/0/Download/abcFichaObrasUpdate/update.apk
安装->;
Java.IO.File updatePath = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString(), "abcFichaObrasUpdate");
Java.IO.File toInstall = new Java.IO.File(updatePath, "update.apk");
Android.Net.Uri apkUri = Android.Support.V4.Content.FileProvider.GetUriForFile(Application.Context,
AppInfo.PackageName + ".provider",
toInstall);
Intent intent = new Intent(Intent.ActionView);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
intent.SetFlags(ActivityFlags.ClearTop);
intent.SetFlags(ActivityFlags.NewTask);
intent.PutExtra(Intent.ExtraNotUnknownSource, true);
intent.SetData(apkUri);
Application.Context.StartActivity(intent);
AndroidManifest.xml
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.abcbraga.abcfichaobras.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" />
</provider>
filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<files-path
name="files"
path="." />
</paths>
但当它获得apkUri时,它会将其转换为{content://com.abcbraga.abcfichaobras.provider/external/Download/abcFichaObrasUpdate/update.apk}
当它尝试启动安装活动时,它只是抛出错误,我认为问题出在URI中。
推荐答案
为使其正常工作,我更改了提供程序
来自Android Support v4
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.abcbraga.abcfichaobras.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" />
</provider>
至androidx核心内容
<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/file_provider_paths" />
</provider>
这篇关于Xamarin Android可编程下载安装APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!