PhoneGap的Android的获取应用程序的版本code [英] PhoneGap Android get the app version code
问题描述
我建立一个PhoneGap的Android应用程序与jQueryMobile。
I am building a phonegap android app with jQueryMobile.
我想建立一个函数来检查与Web服务的应用程序的版本code,以建议更新,如果应用程序已经过时,促使对谷歌Play商店。
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.
这是我的搜索,它看起来像PhoneGap的不具有找到的版本code的方法的。
From my searches, it looks like PhoneGap does not have a method for finding the version code.
有没有办法做到这一点:
Is there a way to do it:
- 没有在Java类中写任何东西?
- 在无需手动保存一个更新的变量,每次我推一个新的版本?
或者,如果写的类是必不可少的,如何从PhoneGap的称呼呢?
Or, if writing in the classes is essential, how do I call it from PhoneGap?
编辑:我编辑与工作解决方案公认的答案!我希望每个人都可以从中获利。
修改(2):这里是完整的插件回购有说明书,随意叉吧:) https://开头github.com/gcatalfamo/Version
EDIT (2): and here is the full plugin repo with instructions, feel free to fork it :) https://github.com/gcatalfamo/Version
推荐答案
我知道你问的w / o编写Java类,但这种或那种方式,你需要写一个。你需要写 PhoneGap的插件
安德烈说。
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.
创建一个自定义的 PhoneGap的插件
:
在您的应用程序包创建一个新类:
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;
}
}
在plugins.xml,添加以下行:
In the plugins.xml, add the following line:
<plugin name="Version" value="your.package.Version" />
在你的 deviceready
事件中添加以下code:
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();
}
然后,您可以通过执行获得的版本code属性:
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
}
);
这篇关于PhoneGap的Android的获取应用程序的版本code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!