0

我以前从未使用过 java 开发,我正试图弄清楚如何通过 phonegap 插件通过 javascript 启动 Google Navigation 应用程序。

我正在尝试修改 phonegap 示例 java 类,但没有任何运气。这是课程。

appname/src/PhoneNavigator.java

package com.phonegap.plugin.phoneNavigator;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;

/**
 * This class echoes a string called from JavaScript.
 */
public class PhoneNavigator extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("doNavigate")) {
            String message = args.getString(0); 
            this.doNavigate(message,callbackContext);
            return true;
        }
        return false;
    }

    private void doNavigate(String location, CallbackContext callbackContext) {
        if (location != null && location.length() > 0) { 
            callbackContext.success(location);
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + location)); 
            startActivity(i);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void startActivity(Intent i) {
        // TODO Auto-generated method stub

    }
}

然后我有以下javascript函数

function doNavigate(str){

    str = encodeURIComponent(str);

    cordova.exec(function(winParam) { alert(winParam);}, function(error) { alert(error);}, "PhoneNavigator",
             "doNavigate", [str]);
}

当我在我的应用程序中运行 javascript 函数时,我收到一条警告说“无效操作”。在我见过的所有示例中,它们都以“startAcitivity(i);”结尾。当我尝试这样做时,eclipse 告诉我我没有该方法可用。我不确定我做错了什么。

4

1 回答 1

0

想通了以下。

package com.phonegap.plugin.phoneNavigator;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;

/**
 * This class echoes a string called from JavaScript.
 */
public class PhoneNavigator extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("doNavigate")) {
            String message = args.getString(0); 
            this.doNavigate(message,callbackContext);
            return true;
        }
        return false;
    }

    private void doNavigate(String location, CallbackContext callbackContext) {
        if (location != null && location.length() > 0) { 
            callbackContext.success(location);
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + location)); 
            this.cordova.getActivity().startActivity(i);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }


}
于 2013-01-18T20:37:40.407 回答