9

I am trying to install an APK from a URL. This is my code:

Intent promptInstall = new Intent(android.content.Intent.ACTION_VIEW);
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.setDataAndType(Uri.parse("http://10.0.2.2:8081/MyAPPStore/apk/Teflouki.apk"), "application/vnd.android.package-archive" );

startActivity(promptInstall);

But I have this problem:

05-10 15:09:29.511: ERROR/AndroidRuntime(1668): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://10.0.2.2:8081/MyAPPStore/apk/Teflouki.apk typ=application/vnd.android.package-archive flg=0x10000000 }

Thanks in advance.

stkent
  • 19,772
  • 14
  • 85
  • 111
lady android
  • 217
  • 3
  • 4
  • 9
  • 1
    Install or uninstall APKs by Uri: http://stackoverflow.com/questions/6813322/install-uninstall-apks-programmatically-packagemanager-vs-intents – Håvard Geithus Jul 27 '12 at 18:35
  • I also want to install android application without downloading from my own server. Did you find the solution? – Manoj Oct 27 '12 at 06:43

3 Answers3

4

You should download xxx.apk in storage before install by:

Intent promptInstall = new Intent(android.content.Intent.ACTION_VIEW);
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.setDataAndType(Uri.parse("storage/xxx.apk"), "application/vnd.android.package-archive" );
startActivity(promptInstall);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

This won't help if the app is not available on the mearketplace, but in case it is:

Uri marketUri = Uri.parse("market://search?q=pname:com.appmaker.tefloukipackage");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
try {
    context.startActivity(marketIntent);
} catch (ActivityNotFoundException ex) {
    showAlertDialog(context, "Error", "Could not launch the market application.", true, null);
}
stkent
  • 19,772
  • 14
  • 85
  • 111
kellogs
  • 2,837
  • 3
  • 38
  • 51
  • 2
    Intent promptInstall = new Intent("android.intent.action.VIEW",Uri.parse("10.0.2.2:8081/MyAPPStore/apk/Teflouki.apk";)); startActivity(promptInstall); – lady android May 12 '11 at 14:27
  • 1
    @ladyandroid how did you manage to open the installer? If I use that the phone only downloads the application but there is no prompt to install it. – htafoya Apr 18 '12 at 15:04
  • @htafoya Did you ever get yours to launch the installation? – aknatn Jun 20 '12 at 04:09
  • This just does the download. I can't get the installer to launch after the download either. Anybody got it to install automatically? – MSquare May 22 '13 at 10:29
  • @MSquare, it can not prompt to install the apk automatically. The end user MUST click it to start installatioin. – suitianshi Apr 23 '14 at 08:14
2

Follow along.

In your module manifest, add

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

//Inside application block
<application>
    <provider
        android:name="androidx.core.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_path"/>
    </provider>
</application>

In your module res/xml folder, if not create this folder, with file provider_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>

And use this method.

private fun updateApplication(activity: Activity) {
    //This will get you the root directory path
    val externalStoragePublicDirectory: String =
        Environment.getExternalStorageDirectory().path

    val externalStoragePublicDirectoryFile =
        File(externalStoragePublicDirectory, "MyApp" + ".apk")

    val uri = FileProvider.getUriForFile(
        activity.applicationContext,
        activity.applicationContext.packageName + ".provider",
        externalStoragePublicDirectoryFile
    )

    val installAppIntent = Intent(Intent.ACTION_VIEW)
        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
        .setDataAndType(
            uri,
            "application/vnd.android.package-archive"
        )
        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        .putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
    activity.startActivity(installAppIntent)
    //This will close your app, remove if not needed
    exitProcess(0)
}

Important, also go to your phone settings, search for unknown sources, enable it in old devices, but in new devices, search for your app and allow it the permission to install new app packages. Only then you will get the pop up to install the app.

Mohammed Uzair
  • 145
  • 1
  • 11