0

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 ?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I found that I am passing an empty Bundle. Thanks
1

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;
   }
}

1 Comment

Although it wasn't the answer, but it helped a lot. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.