android phonegap插件启动导航应用程序

时间:2013-01-18 20:19:22

标签: android cordova

我以前从未开发过java,我试图通过javascript通过phonegap插件找出如何启动Google导航应用程序。

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

应用程序名称/ 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告诉我,我没有那种方法可用。我不确定我做错了什么。

1 个答案:

答案 0 :(得分: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.");
        }
    }


}