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:
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
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.
The build number is reported in Settings > About phone. For Android Oreo, it is shown in Settings > System > About phone.
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.
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
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With