Xamarin Android 7+ 以编程方式安装 APK [英] Xamarin Android 7+ install APK programmatically

查看:69
本文介绍了Xamarin Android 7+ 以编程方式安装 APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安装已下载到 Android 7 中的下载文件夹的 .apk.

I am trying to install an .apk I have downloaded to the downloads folder in Android 7.

我已经尝试了许多 StackOverflow 帖子和这里推荐的方法 https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en通过使用 FileProvider:

I have tried the way recommended in a number of StackOverflow posts and here https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en by using a FileProvider:

File file = new File(fileUri);
//using Android.Support.V4.Content;
var downloadUri = FileProvider.GetUriForFile(context,context.ApplicationContext.PackageName + ".com.package.name.provider", file);
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);

AndroidManifest.xml

AndroidManifest.xml

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<application android:label="Settings" android:icon="@drawable/Icon" android:theme="@style/myTheme">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.com.package.name.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

provider_paths.xml

provider_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." /> 
</paths>

downloadUri"看起来像:content://com.package.name.com.package.name.provider/external_files/Download/Sensors%2520Multitool_1.3.0_apk-dl​​.com.apk"

The "downloadUri" looks like: "content://com.package.name.com.package.name.provider/external_files/Download/Sensors%2520Multitool_1.3.0_apk-dl.com.apk"

安装窗口弹出的错误是:解析包时出现问题".

The error when the installation window pops up is: "There was a problem parsing the package".

我已经通过在下载文件夹中点击它来安装这个包,它安装得很好,我也尝试过其他 .apk 的同样问题.

I have installed this package by clicking on it in the downloads folder and it installs fine, I have also tried other .apk's with the same issue.

推荐答案

解析包的问题似乎是由于包名称Sensors%2520Multitool_1.3.0_apk-dl​​.com.apk"中的空格造成的.

It appears that the issue in parsing the package was due to the space in the package name "Sensors%2520Multitool_1.3.0_apk-dl.com.apk".

删除空间后,软件包安装正确.

As soon as the space was removed the package installed correctly.

这篇关于Xamarin Android 7+ 以编程方式安装 APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆