我需要编写一个cordova插件来检索Android应用程序的应用程序名称,版本代码和版本名称。请尽快回复...在此先感谢:)
这是我的代码....获取无效操作错误消息。
在 My.js
中var AppInfo = function() {};
AppInfo.prototype.getVersionName = function(successCallback, failureCallback) {
return cordova.exec(
successCallback,
failureCallback,
'AppInfo',
'GetVersionName',
[]
);
};
在 AppInfo.java (插件)
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;
}
}
在 .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>
最后在plugin.xml中设置权限和插件
答案 0 :(得分:2)
此帮助程序函数使用应用程序名称,程序包名称,版本号和代码以及图标检索所有已安装的应用程序。方法getPackages()
会返回包含所有应用的ArrayList
。
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; }
我希望它会对你有所帮助。
由于
答案 1 :(得分:1)
您可以通过扩展PhoneGap的Plugin类并在插件中添加包Info类来实现Package Manager的插件,并实现逻辑以获取已安装的应用程序。