3

当我在手机上打开应用程序并将此名称传递给 url 时,我想使用 phonegap 获取我的应用程序的名称。有没有办法获取应用名称?

我目前在应用程序打开时执行此操作,但我需要将应用程序的名称传递到 URL 的末尾:

location.href="http://samplewebsite.com/randomstuff.php?name=" . /*neednameofapp*/;
4

1 回答 1

3

我想做同样的事情,所以我写了一个插件来做。你可以从这里下载我的 Eclipse 测试项目和编译好的 APK ,或者复制下面的插件源代码:

应用信息.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;
    }
}

应用信息.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>
于 2013-07-01T21:30:17.397 回答