If the server connection was successful, and the main page was retrieved and parsed, you will receive
------------------------ Communicate between Android-Java & JavaScript ----------------------------
While developing mobile app sometimes we need to use webview & HTML page & we want to communicate between JS & Java. Below is example demonstrates how to call JS function when button from android is clicked and also how to call android native method by click event on button from HTML page.
WebView.onPageFinished
callback, so you also need to have this in your WebViewClient
subclass:public class MyWebViewClient extends WebViewClient {
...
@Override
public void onPageFinished(WebView view, String url) {
// Make a note that the page has finished loading.
}
...
}
------------------------ Communicate between Android-Java & JavaScript ----------------------------
Android Java & JavaScript Bridge
While developing mobile app sometimes we need to use webview & HTML page & we want to communicate between JS & Java. Below is example demonstrates how to call JS function when button from android is clicked and also how to call android native method by click event on button from HTML page.
private void initWebView(){
private WebView webView; private JsHandler _jsHandler;
// webView = (WebView)findViewById(R.id.webviewId);//Tell the WebView to enable javascript execution.
webView.getSettings().setJavaScriptEnabled(true); webView.setBackgroundColor(Color.parseColor("#808080")); //Set whether the DOM storage API is enabled.
webView.getSettings().setDomStorageEnabled(true); //setBuiltInZoomControls = false, removes +/- controls on screen
webView.getSettings().setBuiltInZoomControls(false); webView.getSettings().setPluginState(WebSettings.PluginState.ON); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setAppCacheMaxSize(1024 * 8); webView.getSettings().setAppCacheEnabled(true); _jsHandler = new JsHandler(this, webView); webView.addJavascriptInterface(_jsHandler, "JsHandler"); webView.getSettings().setUseWideViewPort(false); webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub
super.onPageStarted(view, url, favicon); // Toast.makeText(MainActivity.this, "url "+url, Toast.LENGTH_SHORT).show(); } @Override
public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); //http://192.168.1.7/sportobuddy/public/customformevent/submit-success/66 //http://192.168.1.7/sportobuddy/public/get-event-form/66/292/304/1 _jsHandler.jsFnCall(url); if (url.equalsIgnoreCase("http://192.168.1.7/sportobuddy/public/customformevent/submit-success/66")){ Toast.makeText(MainActivity.this, "url finish "+url, Toast.LENGTH_SHORT).show(); webview_layout.setVisibility(View.GONE); content_layout.setVisibility(View.VISIBLE); } // Toast.makeText(MainActivity.this, /*"Width " + view.getWidth() +" *** " + "Height " + view.getHeight()*/ "finished", Toast.LENGTH_SHORT).show(); } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { // TODO Auto-generated method stub super.onReceivedSslError(view, handler, error); //Toast.makeText(TableContentsWithDisplay.this, "error "+error, Toast.LENGTH_SHORT).show(); } }); // these settings speed up page load into the webview
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); webView.requestFocus(View.FOCUS_DOWN); // load the main.html file that kept in assets folder
webView.loadUrl("http://192.168.1.7/sportobuddy/public/get-event-form/66/292/304/1"); }
public class JsHandler { Activity activity; String TAG = "JsHandler"; WebView webView; public JsHandler(Activity _contxt, WebView _webView) { activity = _contxt; webView = _webView; } /** * This function handles call from JS */
public void jsFnCall(String jsString) { showDialog(jsString); } /** * This function handles call from Android-Java */
public void javaFnCall(String jsString) { final String webUrl = "javascript:diplayJavaMsg('"+jsString+"')"; // Add this to avoid android.view.windowmanager$badtokenexception unable to add window if(!activity.isFinishing()) // loadurl on UI main thread
activity.runOnUiThread(new Runnable() { @Override
public void run() { webView.loadUrl(webUrl); } }); } /** * function shows Android-Native Alert Dialog */
public void showDialog(String msg){ AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); alertDialog.setTitle("activity.getString(R.string.app_dialog_title)"); alertDialog.setMessage(msg); alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,"texttt", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"cancel", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.show(); } }