简体   繁体   English

提示用户安装 APK

[英]Prompting user to install APK

I'm building my own update mechanism for my Android app (I'm not using Play Store).我正在为我的 Android 应用程序(我没有使用 Play 商店)构建自己的更新机制。 I can successfully download a new apk using DownloadManager .我可以使用DownloadManager成功下载新的 apk。 Once the download is finished, I would like to prompt the user to install the apk.下载完成后,我想提示用户安装 apk。 I have tried the following approach:我尝试了以下方法:

    File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
    String test = file.getAbsolutePath();
    Intent promptInstall = new Intent(Intent.ACTION_VIEW)
            .setDataAndType(Uri.parse("content://" + file.getAbsolutePath()),
                    "application/vnd.android.package-archive");
    context.startActivity(promptInstall);

Unfortunately, this does not work at all.不幸的是,这根本不起作用。 The file is there but there is no installation prompt.该文件在那里,但没有安装提示。 What is going wrong?出了什么问题? Do I also have to adapt the second argument to setDataAndType ?我是否还必须将第二个参数调整为setDataAndType It should work on SDK 23 to 29.它应该适用于 SDK 23 到 29。

Edit :编辑

I have now tried the following approach:我现在尝试了以下方法:

    File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");

    Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
    if (Build.VERSION.SDK_INT >= 24) {
        fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
    }
    Intent promptInstall = new Intent(Intent.ACTION_VIEW, fileUri);
    promptInstall.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
    promptInstall.setDataAndType(fileUri, "application/vnd.android.package-archive");
    promptInstall.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
    promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(promptInstall);

In manifest I have now the following:在清单中,我现在有以下内容:

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

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="ir.greencode"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/path" />
        </provider>

And I have created the path.xml file:我已经创建了 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>

But now I'm getting the error message:但现在我收到错误消息:

java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.testapp.provider java.lang.IllegalArgumentException:找不到具有权限 com.testapp.provider 的提供者的元数据

My downloaded APK is located in /storage/emulated/0/Android/data/com.testapp/files/Download/app-debug.apk .我下载的 APK 位于/storage/emulated/0/Android/data/com.testapp/files/Download/app-debug.apk That means file is pointing to that directory.这意味着file指向该目录。 In the end, I would like to store the apk either on internal storage or SD storage (depending on where more space is available).最后,我想将 apk 存储在内部存储或 SD 存储中(取决于有更多可用空间)。

How can I resolve the problem?我该如何解决这个问题?

Possible duplicate of Install Application programmatically on Android 在 Android 上以编程方式安装应用程序的可能副本

I think this answer from that thread should work:我认为该线程的这个答案应该有效:

https://stackoverflow.com/a/54424223/9490453 https://stackoverflow.com/a/54424223/9490453

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在设备上安装“捆绑”应用程序(apk),只提示用户一次 - How can I install a “bundle” of applications (apk's) on device with prompting user only once 安装.apk,无需用户操作 - Install .apk without User action 在真实设备上调试 Flash Builder 4 移动应用程序时提示 INSTALL_FAILED_INVALID_APK - Prompting INSTALL_FAILED_INVALID_APK while debugging Flash Builder 4 mobile app on real device Android安装apk而无需用户许可 - Android install apk without asking user permission 提示用户启用GPS - Prompting User to Enable GPS android中的失败[INSTALL_FAILED_USER_RESTRICTED: Invalid apk] - Failure [INSTALL_FAILED_USER_RESTRICTED: Invalid apk] in android 无法启动 APK 文件以便用户安装它,Android 9 以后? - Cannot launch APK file so user can install it, Android 9 onward? Android权限未提示用户进行调试 - Android permissions not prompting the user in debug 在本地phonegap上签名.apk文件而无需密码提示 - Sign .apk file without password prompting on local phonegap Github 动作包 Flutter APK,提示应用签名不一致 - Github Action packages Flutter APK, prompting inconsistent app signatures
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM