Android: how to open an apk file after downloading for auto-update?
Asked Answered
P

2

10

I have an application that I'd like to add auto-update functionality (it's not in the marketplace). I have all the code in place that would check to see if there is an update available and then I call something like this:

Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(Configuration.APK_URL));
c.startActivity(intent);  

which starts downloading the file. Is there a way that I can programmatically tell it to "open" the file to begin the installation process without the user having to go to the downloads and clicking on it?

Peregrinate answered 25/8, 2010 at 16:29 Comment(0)
S
13

The answer above was for pre-API-24.

If your app targets API 24 or more (and it should), you need to use something else (otherwise you get FileUriExposedException, as described here) :

    File apkFile = new File(...);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri fileUri = android.support.v4.content.FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", apkFile);
    intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);

provider_paths.xml:

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

where YOUR_PACKAGE_NAME is your app's package name.

manifest:

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


<provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="${applicationId}.provider"
  android:exported="false"
  android:grantUriPermissions="true">
    <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/provider_paths"/>
</provider>
Shredding answered 2/12, 2016 at 15:57 Comment(6)
I followed this approach but when downloading is finished nothing happensOgham
Not related to downloading. You need to do this on the file path.Shredding
I do that after downloading is finished and I use the file path but nothing happens :||Ogham
@Ogham Perhaps you are missing this in the manifest: <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />Shredding
You are right it was because of the missing the REQUEST_INSTALL_PACKAGES permission, thank youOgham
@Ogham It was added after I wrote the answer, so I've updated my answer when I noticed it's missing and you told me about it.Shredding
W
7

This will start the installation process

File apkFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/packageName.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
startActivity(intent);
Woad answered 25/8, 2010 at 16:50 Comment(1)
This won't work anymore, if your app targets API 24 or more. See my answer belowShredding

© 2022 - 2024 — McMap. All rights reserved.