Last Updated: February 25, 2016
·
3.976K
· bboydflo

Install Intent plugin in Phonegap 3.0

Hi, I am working on a Phonegap project in Android where among others I have a page with a button that will trigger a download process to sdcard/download/ and after that an installation process.

In this tip I will write my solution that works at the moment. There are probably better solutions, but I am still learning all these technologies and I am happy that at least works.

So, the first step would be to create a Java class in your project that extends CordovaPlugin class. I called it "InstallIntent.java". It is a very simple class, where all that matters is the "execute" method:

import java.io.File;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class InstallIntent extends CordovaPlugin {

    @Override
    public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) {
        if (action.equals("install")) {
            cordova.getThreadPool().execute(new Runnable() {//
                @Override
                public void run() {
                    String apkPath = Environment.getExternalStorageDirectory().toString() + "/download/YourAPK.apk";
                    Intent install = new Intent(Intent.ACTION_VIEW); 
                    install.setDataAndType(Uri.fromFile(new File(apkPath)),"application/vnd.android.package-archive");
                    cordova.getActivity().startActivity(install);
                    Log.d("my tag", "after the install action");
                    //showToast("App cache is deleted.","short");
                }
            });
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
        return true;
    } else {
            Log.e("PhoneGapLog", "CacheCleaner Plugin: Error: " +     PluginResult.Status.INVALID_ACTION);
            callbackContext.sendPluginResult(new     PluginResult(PluginResult.Status.INVALID_ACTION));
            return false;
        }
    }
}

The action in this case is called "install" and is important, as it will be called from Javascript.

In your Phonegap project in the particular assets/www/js folder create install-intent.js

cordova.define("cordova/plugin/installation", function (require, exports, module) {
    var exec = require("cordova/exec");
  module.exports = {
        install: function (win, fail) {
            exec(win, fail, "InstallIntent", "install", []); //InstallIntent is the name of the Java class and install is the method called from the front end side. 
        }
    };
});

In the manifest file I included the following permissions:

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

In your index.html include cordova.js, install-intent.js files and the file with your custom scripts, for example custom.js where you can call your install_intent like this:

var installation = cordova.require("cordova/plugin/installation");
installation.install(function() {
        console.log("PhoneGap Plugin: InstallIntent: callback success");
    },
    function() {
        console.log("PhoneGap Plugin: InstallIntent: callback error");
    }
);

In the end add the following line to your config.xml (the one under ProjectName\platforms\android\res\xml)

<feature name="InstallIntent">
    <param name="android-package" value="com.your.package.name.InstallIntent" />
</feature>

Again, this plugin will trigger an installation process of .apk file from external directory / download. I hope it helps anybody.

3 Responses
Add your response

thanks for this! any idea how to get the apk to launch after it gets downloaded and installed?

over 1 year ago ·

Hi, I think It should automatically ask for the opening of the file, and so installing the .apk. I gave up at the Phonegap project and made everything from scratch, so that would not be for much help for you. Florin

over 1 year ago ·

Just to respond, it actually installs the app and then the window disappears completely, it's not like when u normally install an app where it has an "open this app" button. Not too big of a deal I was just curious. Thanks again :)

over 1 year ago ·