I’ve never developed with java before and i’m trying to figure out how to launch the Google Navigation app via javascript via phonegap plugin.
I’m trying to modify the phonegap example java class but not having any luck. Here is the class.
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
}
}
And then i have the following javascript function
function doNavigate(str){
str = encodeURIComponent(str);
cordova.exec(function(winParam) { alert(winParam);}, function(error) { alert(error);}, "PhoneNavigator",
"doNavigate", [str]);
}
When i run the javascript function in my app i get an alert saying “Invalid action”. On all the examples i’ve seen they just end at “startAcitivity(i);”. When i tried to do that, eclipse told me that i didn’t have that method available. I’m not sure what i’m doing wrong.
figured it out with the following.