Tuesday, April 10, 2012

Install Application programmatically on Android


I`m interesting is it possible to install dynamically downloaded apk from custom android application programmatically.



Source: Tips4all

4 comments:

  1. You can easily launch a market link or an install prompt:

    Intent promptInstall = new Intent(Intent.ACTION_VIEW)
    .setData(Uri.parse("file:///path/to/your.apk"))
    .setType("application/vnd.android.package-archive";
    startActivity(promptInstall);


    source

    Intent goToMarket = new Intent(Intent.ACTION_VIEW)
    .setData(Uri.parse("market://details?id=com.package.name"));
    startActivity(goToMarket);


    source

    However, you cannot install .apks without user's explicit permission; not unless the device and your program is rooted.

    ReplyDelete
  2. File file = new File(dir, "App.apk");

    Intent intent = new Intent(Intent.ACTION_VIEW);

    intent.setDataAndType(Uri.fromFile(file),
    "application/vnd.android.package-archive");

    startActivity(intent);


    I had the same problem and after several attempts, it worked out for me this way. I don't know why, but setting data and type separately screwed up my intent.

    ReplyDelete
  3. Well, I digged deeper, and found sources of PackageInstaller application from Android Source.

    https://github.com/android/platform_packages_apps_packageinstaller

    From manifest I found that it requre permission


    And the actual process of installation occurs after confirmation

    Intent newIntent = new Intent();
    newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO, mPkgInfo.applicationInfo);
    newIntent.setData(mPackageURI);
    newIntent.setClass(this, InstallAppProgress.class);
    String installerPackageName = getIntent().getStringExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME);
    if (installerPackageName != null) {
    newIntent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, installerPackageName);
    }
    startActivity(newIntent);

    ReplyDelete
  4. Yes it's possible. But for that you need the phone to install unverified sources. For exemple, slideMe does that. I think the best thing you can do is to check if the application is present and send an intent for the Android Market. you should use something the url scheme for android Market.

    market://details?id=package.name


    I don't know exactly how to start the activity but if you start an activity with that kind of url. It should open the android market and give you the choice to install the apps.

    ReplyDelete