如何从我的Android活动中调用Javascript函数?

时间:2014-01-20 08:43:34

标签: android

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

我试图调用我的Javascript函数:

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

2 个答案:

答案 0 :(得分:1)

你想尝试像

这样的东西
myWebView.loadUrl("javascript:myfunction(" + param1 + "," +  param2 + ")");

其中myfunction是你的javascript函数,param1和param2是你希望传递它的元素。

还要确保在调用任何函数之前已在webview中启用了javascript

myWebView.setJavaScriptEnabled(true);

答案 1 :(得分:0)

我只是添加了这段代码解决了这个问题。

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