【问题标题】:Install or Update Android App Programmatically android 8以编程方式安装或更新 Android 应用程序 android 8
【发布时间】:2018-08-23 10:20:09
【问题描述】:

我正在尝试在 android 8 (Oreo) 设备上以编程方式从我的应用安装应用。

我正在使用以下导致此问题的代码。 我的文件提供者权限定义为

`private static final String AUTHORITY = "com.unicornialab.myapplication.fileprovider";`

我尝试安装的应用程序位于我设备根存储中名为 Alpha1updates 的文件夹中,如第一行所示

`File toInstall = new File(Environment.getExternalStorageDirectory().getPath()
                        +  File.separator + "Alpha1Updates" + File.separator, "update.apk")` 

然后我尝试安装如下所示的应用程序

`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
           // Uri contentUri = FileProvider.getUriForFile(this, "com.unicornialab.myapplication", newFile);
            Uri apkUri = FileProvider.getUriForFile(context, AUTHORITY, toInstall);
//            Intent intent = new Intent(Intent.ACTION_VIEW);
//            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            intent.setData(apkUri);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.startActivity(intent);
        } else {
            Uri apkUri = Uri.fromFile(toInstall);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }`

由于我使用的是android 8设备,代码在

`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {}`

在上面的代码中执行。 我已请求获得安装来自未知来源的软件包的权限,例如

`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (!getPackageManager().canRequestPackageInstalls()) {
                startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), MY_PERMISSIONS_INSTALLFROM_UNKWONSOURCES);
            }
        }`

我的清单文件中包含了这样的提供程序。

`<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>`

xml 文件夹中的 provider_paths 如下所示

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

我可能做错了什么解析包时出现问题

任何帮助将不胜感激。

【问题讨论】:

  • 嗨。你的问题解决了吗?

标签: android-8.0-oreo android-fileprovider android-8.1-oreo


【解决方案1】:

在这个问题中你应该为android oreo添加这个权限

   <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

对于提供者类,我生成了一个类 GenericFileProvider,如下所示:

public class GenericFileProvider extends FileProvider {

private static final String TAG_EXTERNAL_FILES = "external-files-path";
private static final String TAG_EXTERNAL_CACHE = "external-cache-path";

private static final String TAG_ROOT_PATH = "root-path";
private static final String TAG_FILES_PATH = "files-path";
private static final String TAG_CACHE_PATH = "cache-path";
private static final String TAG_EXTERNAL = "external-path";} 

这是我的 xml 代码

 <?xml version="1.0" encoding="utf-8"?>
  <paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="./"/>
</paths>

在这个问题中,我认为您在获取应用程序中的目录地址是错误的,您应该尝试 解决这个问题。

【讨论】:

    猜你喜欢
    • 2012-06-23
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多