install apk from url

8

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.

android
asked on Stack Overflow May 10, 2011 by lady android • edited Sep 24, 2019 by stkent

3 Answers

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);
answered on Stack Overflow Mar 17, 2016 by on the coming • edited Apr 15, 2016 by Remy Lebeau
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);
}
answered on Stack Overflow May 10, 2011 by kellogs • edited Sep 24, 2019 by stkent
0

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.

answered on Stack Overflow Jul 22, 2020 by Mohammed Uzair

User contributions licensed under CC BY-SA 3.0