Ricky
Ricky

Reputation: 384

How do I call a Javascript function from my Android activity?

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            String url = new String("file:///android_asset/Map.html");
            setContentView(R.layout.leaflet_map);
            this.setTitle("Location Map");

            if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                        .permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }

            initComponent();

            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            webSettings.setRenderPriority(RenderPriority.HIGH);
            webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

            // multi-touch zoom
            webSettings.setBuiltInZoomControls(true);
           /* webSettings.setDisplayZoomControls(false);*/
            myWebView.getSettings().setLoadsImagesAutomatically(true);
            myWebView.setWebChromeClient(new WebChromeClient());
            myWebView.setWebViewClient(new WebViewClient());

            myWebView.loadUrl("file:///android_asset/Map.html");
            myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
            myWebView.setWebViewClient(new WebViewClient());
            mapTextView.setText("Hello Map!!!!");

        }

        public void initComponent(){

            generateId();
            getGeoLocation();
    }

Here I'm trying to call my Javascript function:

    public void getGeoLocation() {

            // creating GPS Class object
            gps = new com.getlocation.periscope.GPSTracker(this);

            // check if GPS location can get
            if (gps.canGetLocation()) {
                Log.d("Your Location", "latitude:" + gps.getLatitude()
                        + ", longitude: " + gps.getLongitude());
                Double latitude = gps.getLatitude();
                Double longitude = gps.getLongitude();
                try{
                myWebView.loadUrl("javascript:latLong('"+latitude+"','"+longitude+"')");
                }catch(Exception ex){
                    Toast.makeText(this,"Exception type"+ex, Toast.LENGTH_LONG).show();

                }
            } else {
                // Can't get user's current location
                alert.showAlertDialog(this, "GPS Status",
                        "Couldn't get location information. Please enable GPS",
                        false);
                // stop executing code by return
                return;
            }
            Toast.makeText(
                    this,
                    "latitude:" + gps.getLatitude() + ", longitude: "
                            + gps.getLongitude(),Toast.LENGTH_LONG).show();
        }

Upvotes: 0

Views: 224

Answers (2)

Ricky
Ricky

Reputation: 384

I solved it out just by adding this snippet of code.

 myWebView.setWebViewClient(new WebViewClient() {
       public void onPageFinished(WebView view, String url) {
            getGeoLocation();
         Bundle getbundle = new Bundle();
         String latString = getbundle.getString("latitud");
         String lotString = getbundle.getString("longitud");

             view.loadUrl("javascript:latLong("+latString+","+lotString+")");
        }
    });

Upvotes: 0

MikeIsrael
MikeIsrael

Reputation: 2889

You want to try something like

myWebView.loadUrl("javascript:myfunction(" + param1 + "," +  param2 + ")");

where myfunction is your javascript function and param1 and param2 are the elements you would like to pass it.

also make sure that you have enabled javascript in your webview before calling any functions

myWebView.setJavaScriptEnabled(true);

Upvotes: 1

Related Questions