Android + Phonegap +程序包管理器 [英] Android + phonegap +package manager
问题描述
我需要编写一个cordova插件来检索用于Android应用程序的应用程序名称,版本代码和版本名称。请尽快回复...预先感谢:)
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.
在 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中设置权限和插件
and finally setting permission and Plugin in plugin.xml
推荐答案
此帮助器函数将检索所有已安装的应用程序,包括应用程序名称,程序包名称,版本号和-code以及图标。方法 getPackages()
返回带有所有应用程序的 ArrayList
。
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.
谢谢
这篇关于Android + Phonegap +程序包管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!