title: install / uninstall APKs programmatically (PackageManager vs Intents)
body: <p>My application installs other applications, and it needs to keep track of what applications it has installed. Of course, this could be achieved by simply keeping a list of installed applications. But this should not be necessary! It should be the responsibility of the PackageManager to maintain the installedBy(a, b) relationship. In fact, according to the API it is:</p>
  
  <p>public abstract String <b>getInstallerPackageName</b>(String packageName) - 
  <i>Retrieve the package name of the application that installed a package. This identifies which market the package came from.</i></p>
  
  <h2>The current approach</h2>
  
  <p><b>Install APK using Intent</b></p>
  
  <pre><code>Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
  startActivity(intent);
  </code></pre>
  
  <p><b>Uninstall APK using Intent:</b></p>
  
  <pre><code>Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",
  getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).packageName,null));
  startActivity(intent);
  </code></pre>
  
  <p>This is obviously not the way e.g. Android Market installs / uninstalls packages. They use a richer version of the PackageManager. This can bee seen by downloading the Android source code from the Android Git repository. Below are the two hidden methods that corresponds to the Intent approach. Unfortunately they are not available to external developers. But perhaps they will be in the future?</p>
  
  <h2>The better approach</h2>
  
  <p><b>Installing APK using the PackageManager</b></p>
  
  <pre><code>/**
   * @hide
   * 
   * Install a package. Since this may take a little while, the result will
   * be posted back to the given observer.  An installation will fail if the calling context
   * lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the
   * package named in the package file's manifest is already installed, or if there's no space
   * available on the device.
   *
   * @param packageURI The location of the package file to install.  This can be a 'file:' or a
   * 'content:' URI.
   * @param observer An observer callback to get notified when the package installation is
   * complete. {@link IPackageInstallObserver#packageInstalled(String, int)} will be
   * called when that happens.  observer may be null to indicate that no callback is desired.
   * @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
   * {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
   * @param installerPackageName Optional package name of the application that is performing the
   * installation. This identifies which market the package came from.
   */
  public abstract void installPackage(
          Uri packageURI, IPackageInstallObserver observer, int flags,
          String installerPackageName);
  </code></pre>
  
  <p><b>Uninstalling APK using the PackageManager</b></p>
  
  <pre><code>/**
   * Attempts to delete a package.  Since this may take a little while, the result will
   * be posted back to the given observer.  A deletion will fail if the calling context
   * lacks the {@link android.Manifest.permission#DELETE_PACKAGES} permission, if the
   * named package cannot be found, or if the named package is a "system package".
   * (TODO: include pointer to documentation on "system packages")
   *
   * @param packageName The name of the package to delete
   * @param observer An observer callback to get notified when the package deletion is
   * complete. {@link android.content.pm.IPackageDeleteObserver#packageDeleted(boolean)} will be
   * called when that happens.  observer may be null to indicate that no callback is desired.
   * @param flags - possible values: {@link #DONT_DELETE_DATA}
   *
   * @hide
   */
  public abstract void deletePackage(
          String packageName, IPackageDeleteObserver observer, int flags);
  </code></pre>
  
  <h2>Differences</h2>
  
  <ul>
  <li><p>When using intents the local package manager is not made aware of which application the installation originated from. Specifically, getInstallerPackageName(...) returns null.</p></li>
  <li><p>The hidden method installPackage(...) takes the installer package name as a parameter, and is most likely capable of setting this value.</p></li>
  </ul>
  
  <h2>Question</h2>
  
  <p><b>Is it possible to specify package installer name using intents?</b> 
  <i>(Maybe the name of the installer package can be added as an extra to the installation intent?)</i> </p>
  
  <p><i>Tip: If you want to download the Android source code you can follow the steps described here: Downloading the Source Tree. To extract the *.java files and put them in folders according to the package hierarchy you can check out this neat script: <a href="http://blog.michael-forster.de/2008/12/view-android-source-code-in-eclipse.html">View Android Source Code in Eclipse</a>.</i></p>