简体   繁体   中英

Prompting user to install APK

I'm building my own update mechanism for my Android app (I'm not using Play Store). I can successfully download a new apk using DownloadManager . Once the download is finished, I would like to prompt the user to install the 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 ? It should work on SDK 23 to 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:

<?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

My downloaded APK is located in /storage/emulated/0/Android/data/com.testapp/files/Download/app-debug.apk . That means file is pointing to that directory. In the end, I would like to store the apk either on internal storage or SD storage (depending on where more space is available).

How can I resolve the problem?

Possible duplicate of Install Application programmatically on Android

I think this answer from that thread should work:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM