McLan
McLan

Reputation: 2678

Javascript interface with android not working

I am trying to pass variables to JavaScript from Android. When the map button is clicked, it opens this class with the following onCreate method:

@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    myWebView = (WebView) findViewById(R.id.webview);
    setMyWebviewSettings(myWebView.getSettings());

    myWebView.setWebViewClient(new WebViewClient());
    myWebView.setWebChromeClient(new WebChromeClient());

    Bundle extras = getIntent().getExtras();
    double[] coordinates = setCoordinates(extras);

    myWebView.addJavascriptInterface(new JavaScriptInterface(coordinates), "JSInterface");

    myWebView.loadUrl("http://10.0.0.32:8887/services/maps/examples/basic-example.htm");
}

Here is the setMyWebviewSettings method:

private void setMyWebviewSettings(WebSettings MyWebviewSettings){
    MyWebviewSettings.setAllowFileAccessFromFileURLs(true);
    MyWebviewSettings.setAllowUniversalAccessFromFileURLs(true);
    MyWebviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    MyWebviewSettings.setJavaScriptEnabled(true);
    MyWebviewSettings.setDomStorageEnabled(true);
    MyWebviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    MyWebviewSettings.setBuiltInZoomControls(true);
    MyWebviewSettings.setAllowFileAccess(true);
    MyWebviewSettings.setSupportZoom(true);
}

Then here is JavaScriptInterface:

public class JavaScriptInterface {

    double lat;
    double lon;
    double pan;

    JavaScriptInterface(double[] coordinate) {
        this.lat = coordinate[0];
        this.lon = coordinate[1];
        this.pan = coordinate[2];
    }

    public double getLatValue() {
        return lat;
    }

    public double getLonValue() {
        return lon;
    }

    public double getPanValue() {
        return pan;
    }
}

Here is setCoordinates method:

private double[] setCoordinates(Bundle extras) {

    double[] coordinates = new double[3];

    coordinates[0] = 45.9049;
    coordinates[1] = 13.3177;
    coordinates[2] = 15;

    if (extras != null) {
        coordinates[0] = extras.getDouble("lat");
        coordinates[1] = extras.getDouble("lon");
        coordinates[2] = extras.getDouble("pan");
    }
    return coordinates;
}

Everything works except that it doesn't load the map. The Bundle extras is Null. what to do to fix this ?

Upvotes: 0

Views: 1493

Answers (2)

FrankR
FrankR

Reputation: 206

Make sure you are passing the values when creating the intent that starts the second activity. If the bundle is null then it means they aren't contained in the intent.

Upvotes: 1

justHooman
justHooman

Reputation: 3054

From this doc, I think that maybe you are missing the @JavascriptInterface annotation:

public class JavaScriptInterface {
   @JavascriptInterface
   public double getLatValue() {
      return lat;
   }

   @JavascriptInterface
   public double getLonValue() {
      return lon;
   }

   @JavascriptInterface
   public double getPanValue() {
      return pan;
   }
}

Upvotes: 1

Related Questions