我想做同樣的事情,所以我寫了一個插件去做吧。您可以下載我的Eclipse測試項目和編譯APK from here,或複製以下插件的源代碼:
AppInfo.java
package org.apache.cordova.plugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.pm.PackageInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
public class AppInfo extends CordovaPlugin {
public final String ACTION_GET_VERSION = "GetVersion";
public final String ACTION_GET_NAME = "GetName";
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
ApplicationInfo ai;
CharSequence al;
if(action.equals(ACTION_GET_VERSION)) {
try {
PackageInfo packageInfo = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, packageInfo.versionName));
}
catch (NameNotFoundException nnfe) {
Log.e("AppInfoPlugin", "Errpr occurred calling plugin: " + nnfe.getMessage());
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return false;
}
} else if(action.equals(ACTION_GET_NAME)) {
try {
ai = packageManager.getApplicationInfo(this.cordova.getActivity().getPackageName(), 0);
al = packageManager.getApplicationLabel(ai);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, (String) al));
}
catch (NameNotFoundException nnfe) {
Log.e("AppInfoPlugin", "Errpr occurred calling plugin: " + nnfe.getMessage());
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return false;
}
}
callbackContext.success();
return true;
}
}
appinfo.js
cordova.define("cordova/plugin/appinfo", function(require, exports, module) {
var exec = require('cordova/exec');
var AppInfo = function() {};
AppInfo.prototype.GetVersion = function(successCallback,failureCallback) {
exec(successCallback, failureCallback, 'AppInfo', 'GetVersion', []);
}
AppInfo.prototype.GetName = function(successCallback,failureCallback) {
exec(successCallback, failureCallback, 'AppInfo', 'GetName', []);
}
var appinfo = new AppInfo();
module.exports = appinfo;
});
然後像這樣的index.html:
<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova-2.8.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="appinfo.js"></script>
<script type="text/javascript">
function deviceready() {
console.log("Device ready");
cordova.require('cordova/plugin/appinfo').GetName(
function(name) {
$('#name').html(name);
},
function(error) {
console.error("Error retrieving app name: "+error);
}
);
cordova.require('cordova/plugin/appinfo').GetVersion(
function(version) {
$('#version').html(version);
},
function(error) {
console.error("Error retrieving app version: "+error);
}
);
}
document.addEventListener("deviceready", deviceready, true);
</script>
</head>
<body>
<h1>AppInfo</h1>
<p>App name: <span id="name"></span></p>
<p>App version: <span id="version"></span></p>
</body>
</html>
幫助...我...請... – user2463517