2013-01-18 38 views
0

我从来没有用java开发过,我想弄清楚如何通过手机插件通过JavaScript启动谷歌导航应用程序。android手机插件来启动导航应用程序

我想修改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告诉我,我没有可用的方法。我不确定我做错了什么。

+0

晚会晚了,但现在有[phonegap-launch-navigator](https://github.com/dpa99c/phonegap-launch-navigator) – DaveAlden

回答

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."); 
     } 
    } 


}