Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap Android get the app version code

I am building a phonegap android app with jQueryMobile.

I want to build a function that checks the version code of the application with a webservice in order to suggest to update, if the app is outdated, prompting to the google play store.

From my searches, it looks like PhoneGap does not have a method for finding the version code.

Is there a way to do it:

  • without writing anything in the Java classes?
  • without manually storing an updated variable everytime I push a new version?

Or, if writing in the classes is essential, how do I call it from PhoneGap?

EDIT: I edited the accepted answer with a working solution! I hope everybody can profit from it.

EDIT (2): and here is the full plugin repo with instructions, feel free to fork it :) https://github.com/gcatalfamo/Version

like image 465
gcatalfamo Avatar asked Mar 02 '13 19:03

gcatalfamo


People also ask

How do I find the version of an app on my Android?

Android System SettingsOnce in Settings, tap Apps and notifications (or your phone manufacturer's name it). When you're in there, tap See all xyz apps (where xyz is the number of apps you have installed). Now scroll down until you find the app you want to find the version for. Then tap it in the list.

How do I find the build number on my Android?

The build number is reported in Settings > About phone. For Android Oreo, it is shown in Settings > System > About phone.

What is version code and version name?

The version code is an incremental integer value that represents the version of the application code. The version name is a string value that represents the “friendly” version name displayed to the users.


1 Answers

I know you asked w/o writing Java Classes but one way or another you need to write one. You need to write PhoneGap Plugin as Andre stated.

Create a custom PhoneGap plugin:

Create a new class in your app's package:

package com.phonegap.plugins; //your package name

import org.json.JSONArray;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;

public class Version extends Plugin {

    public final String ACTION_GET_VERSION_NAME = "GetVersionName"; //not used in this version
    public final String ACTION_GET_VERSION_CODE = "GetVersionCode";

    @Override
    public PluginResult execute(String action, JSONArray args, String callbackId) {
        PluginResult result = new PluginResult(Status.INVALID_ACTION);
        PackageManager packageManager = this.cordova.getActivity().getPackageManager();
        if(action.equals(ACTION_GET_VERSION_CODE)) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(this.cordova.getContext().getPackageName(), 0);
                result = new PluginResult(Status.OK, packageInfo.versionCode);
            }
            catch (NameNotFoundException nnfe) {
                result = new PluginResult(Status.ERROR, nnfe.getMessage());
            }
        }

        return result;
    }
}

In the plugins.xml, add the following line:

<plugin name="Version" value="your.package.Version" />

In your deviceready event add the following code:

var Version = function() {};
Version.prototype.getVersionCode = function(successCallback, failureCallback) {
    return cordova.exec(successCallback, failureCallback, 'Version', 'GetVersionCode', []);
};

if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.version) {
    window.plugins.version = new Version();
}

Then, you can get the versionCode attribute by doing:

window.plugins.packageManager.getVersionCode(
    function(versionCode) {
        //do something with versionCode
    },
    function(errorMessage) {
        //do something with errorMessage
    }
);
like image 130
Vikalp Patel Avatar answered Oct 13 '22 23:10

Vikalp Patel