Install and Uninstall Android applications with PackageInstaller

This is actually very simple.

See PackageInstaller code here:http://android.git.kernel.org/?p=platform/packages/apps/PackageInstaller.git;a=tree;h=refs/heads/donut;hb=refs/heads/donut

Intent filters for such actions are:

<activity android:name=".PackageInstallerActivity">
    <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>
 
<activity android:name=".UninstallerActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.DELETE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="package" />
    </intent-filter>
</activity>

so if you want to install your .apk file from SD card – just write something like that:

String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);

for uninstallation:

Uri packageURI = Uri.parse("package:com.android.myapp");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);

be aware that by default INSTALL_NON_MARKET_APPS option is disabled. You may want to check this option and show user friendly dialog before trying to install the app:

int result = Settings.Secure.getInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
if (result == 0) {
    // show some dialog here
    // ...
    // and may be show application settings dialog manually
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
    startActivity(intent);
}

enjoy

Share
  • Sena Gbeckor-Kove

    Is there any way to be notified when packages have been installed or uninstalled? I want to do some external logging when this happens.

    Thanks
    S

  • villain_dm

    no. I haven’t seen any broadcast events inside Package Installer code. Just ask system for the list of installed app every N seconds instead:)

  • Piwaï

    Thanks for the tip, and thanks Google for showing me that page!

    I also realized something by reading this : http://android.git.kernel.org/?p=platform/packages/apps/PackageInstaller.git;a=blob;f=src/com/android/packageinstaller/PackageInstallerActivity.java;h=9b487d13ff4d1b6a8e1bfd04becc2448024cafee;hb=refs/heads/donut (thanks for the link)

    => we DO NOT need to use the sd card to store the APK. All you need to do is something like :

    OutputStream out = context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);

    //[...] copy the file downloaded from the net to the output stream

    File realFilePath = context.getFileStreamPath(fileName);

    Intent i = new Intent();
    i.setAction(Intent.ACTION_VIEW);
    i.setDataAndType(Uri.fromFile(realFilePath ), “application/vnd.android.package-archive”);
    context.startActivity(i);

  • Sameer

    Hi,

    I am getting a Parse Error: There is a problem parsing this package. (I can install the application into the emulator by running it independently) and even added permissions:

    Any help will be most useful. Thanks

    Sameer

  • Ashish

    Hi,

    The above code is not running on my emulator, does it works only on emulator??
    It is giving me “Activity not found exception”.

    please help.

  • villain_dm

    works only on device with installed Android Market

  • Ashish

    @villain_dm

    Thanks a lot !!!!!!!!!!

    will try it on device.

  • Johan

    This code worked perfectly for me on my X10 while running Android 1.6, but now since upgrading to 2.1 I get an error message from the installer saying (translated from Swedish): “The application was not installed”.

    Anyone know why?

  • Johan

    I found out myself. The problem was that I tried to replace an application that was signed with the debug certificate with one signed by my certificate – which is not allowed (maybe it was allowed in 1.6 or maybe I accidentally uploaded the debug apk before)

  • Tej

    That was helpful.
    Is there anyway to install multiple applications but ask the user only once showing that list ? Probably similar to the way apps get updated from market. There is an “Update All” option.

    Similarly, is there any ways to show user a list of apps with the persmissions reqd. and then the user selects install all ?

  • Milos

    Hi all,

    Is there a way to find out whether the uninstalaltion completed successfully?

    Best,
    Milos

  • villain_dm

    no notification… check for installed packages repeately

  • Aizaz

    How to force it to use pakagemanager for uninstall app. ?

  • Pingback: Automatische Deaktivierung/-installation einer App - Android-Hilfe.de

  • vl

    I better use Bloat Freezer – Root which freezes apps on Android instead of uninstall them.

    http://bstdownload.com/reviews/bloat-freezer-root-1/

  • Stéphane

    Nice ! Exactly what I was looking for to help my users switch to a new package name…

    Thx

  • Stéphane

    But I can’t compile package installer, I tried with all android version and get errors about android.com.pm.PackageParser beeing not recognized…

    Can someone help ?

  • Nitan Sehgal

    import android.webkit.MimeTypeMap;

    try
    {
    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File(aFile.getAbsolutePath());
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(myIntent);
    }
    catch (Exception e)
    {
    // TODO: handle exception
    String data = e.getMessage();
    }

  • Lior

    Thank you for a great post!

    If I want to delete some or even all user apps?
    Running this loop, randomly, causes the device to reboot:

    for (int i = 0; i < selected_packages; i++) {

    String current_package = selected_packages_array[i].toString();
    Uri packageURI = Uri.parse("package:"+current_package);
    Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);

    startActivity(uninstallIntent);

    }

    Any ideas?

  • arun

    i used the code . it works fine thanks. in the uninstall process it ask choose unstaller option are packegeinstaller and my app. how to skip this

  • ROhit

    Did you fine any solution for this..?

  • Raju

    Hi ,
    i also have the same problem of parsing package .after adding your code this problem yet not solved.

  • Brahim Guaali

    How to force it to use my app without showing

  • Brahim Guaali

    How to force it to use my app without showing the popup (On Uninstalling)