0

WebView loadUrl runs asynchronous task to load given URL. There is a button which has onClick method with invoking loadURL method. WebView uses JavascriptInterface - particular method is invoked by onload event ( ). I want lock access to object before loadURL and unlock in JavascriptInterface method. Is it enough to put loadUrl in synchronized block?

EDIT:

Loading WebView

        mRenderReceiptWebView = (WebView) findViewById(R.id.receiptWebView);
        mRenderReceiptWebView.getSettings().setJavaScriptEnabled(true);
        mRenderReceiptWebView.getSettings().setPluginState(PluginState.OFF);
        mRenderReceiptWebView.getSettings().setAllowFileAccess(false);

        mRenderReceiptWebView.setVisibility(View.VISIBLE);
        mRenderReceiptWebView.setWebChromeClient(new WebChromeClient(){

            @Override
            public boolean onConsoleMessage(ConsoleMessage cm) 
            {
                Log.d("ShowMote", cm.message() + " -- From line "
                                     + cm.lineNumber() + " of "
                                     + cm.sourceId() );
                return true;
            }
        });
        mRenderReceiptWebView.setDrawingCacheEnabled(true);
               mRenderReceiptWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
  mRenderReceiptWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
  mRenderReceiptWevView.addJavascriptInterface(new JavaScriptInterface(),
  "Android"); receiptWebView.loadUrl(url);

JavaScriptInterface function

public JavaScriptInterface(){

@JavascriptInterface

public void funJS() {}

}

html body onload

<body onload="(function(){ Android.funJS();}());">
dzakens
  • 337
  • 3
  • 14

1 Answers1

0

You can use setWebViewClient and a ProgressDialog to achieve this. Explanation along with code:

web.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap bitmap) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, bitmap);
            //start progress dialog here and setCancellable(false) to dialog
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            //dismiss dialog here progressdialog.dismiss()
        }
    });

Alternately, you can start the progressdialog in onclick method and dismiss it in onPageFinished method.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • But onPageFinished don't ensure that whole content of webpage is loaded. That's why I am using onload event – dzakens Aug 14 '14 at 12:03
  • @dzakens then you could download the html string received from the getrequest to the url and set that string to the webview using asynctask. The above method is the easiest though – Illegal Argument Aug 14 '14 at 12:05
  • but loadUrl call involves creating asynchronous task ? – dzakens Aug 14 '14 at 12:08
  • @dzakens as I told earlier download the html and follow this link there is no asynchronous call in the link here even if there is it will finish in couple of milliseconds http://stackoverflow.com/questions/8987509/how-to-pass-html-string-to-webview-on-android – Illegal Argument Aug 14 '14 at 12:10