Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're using PhoneGap, then create a custom PhoneGap plugin:</p> <p>Create a new class in your app's package:</p> <pre><code>package com.Demo; //replace with your package name import org.json.JSONArray; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; import com.phonegap.api.PluginResult.Status; public class PackageManagerPlugin extends Plugin { public final String ACTION_GET_VERSION_NAME = "GetVersionName"; @Override public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult result = new PluginResult(Status.INVALID_ACTION); PackageManager packageManager = this.ctx.getPackageManager(); if(action.equals(ACTION_GET_VERSION_NAME)) { try { PackageInfo packageInfo = packageManager.getPackageInfo( this.ctx.getPackageName(), 0); result = new PluginResult(Status.OK, packageInfo.versionName); } catch (NameNotFoundException nnfe) { result = new PluginResult(Status.ERROR, nnfe.getMessage()); } } return result; } } </code></pre> <p>In the plugins.xml, add the following line:</p> <pre><code>&lt;plugin name="PackageManagerPlugin" value="com.Demo.PackageManagerPlugin" /&gt; </code></pre> <p>In your <a href="http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html#deviceready">deviceready event</a>, add the following code:</p> <pre><code>var PackageManagerPlugin = function() { }; PackageManagerPlugin.prototype.getVersionName = function(successCallback, failureCallback) { return PhoneGap.exec(successCallback, failureCallback, 'PackageManagerPlugin', 'GetVersionName', []); }; PhoneGap.addConstructor(function() { PhoneGap.addPlugin('packageManager', new PackageManagerPlugin()); }); </code></pre> <p>Then, you can get the versionName attribute by doing:</p> <pre><code>window.plugins.packageManager.getVersionName( function(versionName) { //do something with versionName }, function(errorMessage) { //do something with errorMessage } ); </code></pre> <p>Derived from <a href="http://groups.google.com/group/phonegap/browse_thread/thread/db7f1b09bcae2bdb?pli=1&amp;_done=/group/phonegap/browse_thread/thread/db7f1b09bcae2bdb?pli%3D1%26auth%3DDQAAAIUAAADHTpKQDhprc1z47kIpig_XDwwdijnmrz3n-Ppc8t4iBKMyBAYvlq5sgw85dETkxU2mjmAVjfZHLhsBK1n3EeD2g6eSUgZfmcads4Y8dyICbgsrmRe7qpXo_qN9Foydd7PQwwzB_UGI9kfEyNaGNtxvDWpAwDxrYlcrAFF3vOk0saebVeBNjDv2yIHZaAePwik&amp;fwc=1">here</a> and <a href="http://www.practicaldeveloper.net/2011/09/creating-plugin-with-phonegap-10.html">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload