10

I am new to android and i have been searching the web from last two days for this. I found following links too but i am not getting how and where to implement this code to start automatic installation of the apk file after it downloads completely and how to delete the downloaded apk file after installation. Please help me by guiding me the right way.

how to install apk file programmatically

Invoking activity from APK in another android application

Android install apk programmatically

Install APK programmatically on android

http://www.anddev.org/viewtopic.php?p=23928

EDIT :

i have written this code in manifest file :

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <!-- <uses-permission android:name="android.permission.INSTALL_PACKAGES"/> -->
    <application android:icon="@drawable/biz_logo"
        android:permission="android.permission.WRITE_CONTACTS">
        <activity android:name="com.biz.mlm.Main">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="content" />
                <data android:scheme="file" />
                <data android:mimeType="application/vnd.android.package-archive" />
            </intent-filter>
        </activity>
Community
  • 1
  • 1
Shruti
  • 1
  • 13
  • 55
  • 95
  • Its not clear what you want to do. Do you want to download an APK in your app and install it? Or do you want the phone to detect downloaded APK's and then install them... – slayton Sep 16 '11 at 03:25
  • i want the phone to detect the downloaded apkand then install them please help me on how to do it – Shruti Sep 16 '11 at 05:15

5 Answers5

11

Not possible. As some of those links point out, making an APK that automatically installs itself and deletes the original installer without any further user intervention is called malware.

You can write a program that will download and install arbitrary APKs if the user grants it the relevant permissions, but I'm yet to see a good implementation or documentation for this. Typically you can invoke the default system installer using code like this:

File apkFile = new File({path to APK});
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
    startActivity(intent);
Femi
  • 64,273
  • 8
  • 118
  • 148
  • 1
    Hello fermi , thanks for your answer but i am not getting where to write this code ? should i write in the launcher activity's .java file or create a new java class for it.And if i create a new java class so how should i call that class and where ? what are the permissions i need to add ? – Shruti Sep 16 '11 at 05:17
  • this gives me this error, id dont know why.can somebody please help me.android.os.FileUriExposedException: file:///storage/emulated/0/k5bbsz99d4gbzc0h.apk exposed beyond app through Intent.getData() – Ashana.Jackol Sep 07 '17 at 11:08
  • Might be a version issue: take a look at some of the options in https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed – Femi Sep 11 '17 at 16:30
  • Replace Intent.ACTION_VIEW with this Intent.ACTION_INSTALL_PACKAGE – Narendra.kr Aug 03 '19 at 12:10
  • As a developer, it's really annoying to click *allow install package* every time when I need to update the installation of being developed app. – zwcloud Feb 26 '21 at 08:45
4

Have you tried adding the below permission?

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

Add the above code to your manifest (especially when you are getting security exception).

You can refer to Install apps silently, with granted INSTALL_PACKAGES permission

Hope this works for you.

Community
  • 1
  • 1
Jimmy Ilenloa
  • 1,989
  • 21
  • 21
3
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File("/mnt/sdcard/myapkfile.apk");
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);

The above code will install the myapkfile.apk. You just need to put this code in the onCreate() of the Activity.

Jimmy Ilenloa
  • 1,989
  • 21
  • 21
java dev
  • 1,044
  • 1
  • 11
  • 17
  • Suppose, I have downloaded an application(which I'm building). And I, automatically want it to install it with granted permission. so, the above snippet to put in which onCreate activity? In app any activity, I mean MainActivity? – ssi-anik Oct 01 '15 at 20:11
2

After Download give the path of apk file and it will install automatically in android

Intent intent1 = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent1.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" +filename)), "application/vnd.android.package-archive"); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Narendra.kr
  • 181
  • 3
  • 12
1

Actually this is possible, use BroadcastReceiver to listen when package is added (PACKAGE_ADDED) in the system. Create BroadcastReceiver class, do the follow code :

public void onReceive(Context arg0, Intent arg1) {
    File downLoadApk = new File(Environment.getExternalStorageDirectory(),"New.apk");
        if (downLoadApk.exists()) {
            downLoadApk.delete();
        }

}
kris ho
  • 51
  • 1
  • 9