问题
I need to write a cordova plugin to retrive the application name, version code and version name using for Android application. Please Respond ASAP... Thanks in Advance :)
Here is my Code....getting Invalid Action Error Message.
In My.js
var AppInfo = function() {};
AppInfo.prototype.getVersionName = function(successCallback, failureCallback) {
return cordova.exec(
successCallback,
failureCallback,
'AppInfo',
'GetVersionName',
[]
);
};
In AppInfo.java (Plugin)
package com.cordova.plugin.appInfo;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
public class AppInfo extends CordovaPlugin {
public final String ACTION_GET_VERSION_NAME = "GetVersionName";
Context ctx;
public PluginResult execute(String action, JSONArray args, String callbackId) {
ctx = cordova.getContext();
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;
}
}
In .html
<script>
function onDeviceReady () {
$('#send').bind('click', function () {
alert('Hello World');
window.applicationInfo = new AppInfo();
window.applicationInfo.getVersionName( function(versionName){
alert("versionName" + versionName);
},
function (errorMessage){
alert("Error is "+errorMessage);
}
);
});
}
document.addEventListener("deviceready", onDeviceReady, false);
</script>
and finally setting permission and Plugin in plugin.xml
回答1:
This helper function retrieves all installed apps with the application name, package name, version-number and -code as well as the icons. The method getPackages()
returns an ArrayList
with all the apps.
class PInfo { private String appname = ""; private String pname = ""; private String versionName = ""; private int versionCode = 0; private Drawable icon; private void prettyPrint() { Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode); } } private ArrayList<PInfo> getPackages() { ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */ final int max = apps.size(); for (int i=0; i<max; i++) { apps.get(i).prettyPrint(); } return apps; } private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) { ArrayList<PInfo> res = new ArrayList<PInfo>(); List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); for(int i=0;i<packs.size();i++) { PackageInfo p = packs.get(i); if ((!getSysPackages) && (p.versionName == null)) { continue ; } PInfo newInfo = new PInfo(); newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString(); newInfo.pname = p.packageName; newInfo.versionName = p.versionName; newInfo.versionCode = p.versionCode; newInfo.icon = p.applicationInfo.loadIcon(getPackageManager()); res.add(newInfo); } return res; }
I hope it will help you.
Thanks
回答2:
You can write plugin for the Package Manager by extending PhoneGap's Plugin class and add package Info class there in Plugin and implement logic to get installed applications.
来源:https://stackoverflow.com/questions/14682818/android-phonegap-package-manager