Phonegap如何在应用程序中获取应用程序的名称?

时间:2013-07-01 14:22:17

标签: javascript android cordova

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

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

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

1 个答案:

答案 0 :(得分:3)

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

<强> AppInfo.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;
    }
}

<强> appinfo.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>