2013-01-18 41 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."); 
     } 
    } 


}