2

我需要编写一个 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中设置权限和插件

4

2 回答 2

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; 
 }

我希望它会帮助你。

谢谢

于 2013-02-04T08:50:51.733 回答
1

您可以通过扩展PhoneGap的Plugin类为Package Manager编写插件,并在Plugin中添加包Info类并实现逻辑以获取已安装的应用程序。

于 2013-02-04T09:07:45.300 回答