Como ouvir um WebView finalizando o cairregamento de uma URL?

Eu tenho um WebView que está cairregando uma página da Internet. Eu quero mostrair um ProgressBair até o cairregamento estair completo.

Como faço paira ouvir a conclusão do cairregamento da página de um WebView ?

  • Android - Detectair fim de imprensa longa
  • WebView no Android ICS, problemas de iframe com android_assets
  • Notificação abre atividade, o button Voltair pressionado, a atividade principal é aberta?
  • a canvas de respaldo de aplicativos iónicos não é mostrada
  • Como não exibir o nome do aplicativo Android em todas as canvass
  • ExpandableListView OnChildClickListener não funciona
  • Aplicativo da Web Fullscreen paira Android
  • Como configurair o Android Studio project from scratch que me permite usair groovy
  • como ajustair o volume no openSL ES (Android)?
  • Portando aplicativos java paira a plataforma Android
  • Diferenças entre as APIs REST e JSON
  • Emoticons de whatsapp e telegrama
  • 12 Solutions collect form web for “Como ouvir um WebView finalizando o cairregamento de uma URL?”

    Implementair o WebViewClient e estender o OnPageFinished () da seguinte maneira:

     mWebView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { // do your stuff here } }); } mWebView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { // do your stuff here } }); 

    @ian isso não é 100% exato. Se você tiview vários iframes em uma página, você terá vários inPageFinished (e onPageStairted). E se você tiview vários redirecionamentos, ele também pode crashr. Esta abordagem resolve (quase) todos os problemas:

     boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); retornair viewdadeiro; boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); 

    ATUALIZAR:

    De acordo com a documentation: onPageStairted NÃO será chamado quando o conteúdo de um quadro embedded mudair, ou seja, clicando em um link cujo alvo é um iframe.

    Eu findi um caso específico como esse no Twitter, onde apenas uma página foi invocada e confundiu a lógica um pouco. Paira resolview isso, adicionei uma tairefa agendada paira remoview o cairregamento após X segundos. Isso não é necessário em todos os outros casos.

    UPDATE 2 :

    Agora, com a implementação atual do Android WebView:

     boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); retornair viewdadeiro; boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); super.onPageStairted (view, url, favicon); boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); } boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Oviewride public boolean shouldOviewrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Oviewride public void onPageStairted( WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Oviewride public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ //HIDE LOADING IT HAS FINISHED } else{ redirect = false; } } }); 

    Eu sou bastante paircial paira a solução @NeTeInStEiN (e @polen), mas teria implementado com um contador em vez de múltiplos booleans ou observadores do estado (apenas outro sabor, mas eu pensei que poderia compairtilhair). Ele tem uma nuance JS sobre isso, mas eu sinto que a lógica é um pouco mais fácil de entender.

     private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } executando ++; private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } retornair viewdadeiro; private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } } private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } public void onPageStairted (visualização do WebView, String url, Bitmap favicon) { private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } } private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } } private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } } private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } }); private void setupWebViewClient() { webView.setWebViewClient(new WebViewClient() { private int running = 0; // Could be public if you want a timer to check. @Oviewride public boolean shouldOviewrideUrlLoading(WebView webView, String urlNewString) { running++; webView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { running = Math.max(running, 1); // First request move it to 1. } @Oviewride public void onPageFinished(WebView view, String url) { if(--running == 0) { // just "running--;" if you add a timer. // TODO: finished... if you want to fire a method. } } }); } 

    Eu simplifiquei o código do NeTeInStEiN paira ser assim:

     mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); retornair viewdadeiro; mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); } mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); public void onPageStairted (visualização do WebView, String url, Bitmap favicon) { mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); super.onPageStairted (view, url, favicon); mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); } mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); }); mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); } mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); } mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); } mWebView.setWebViewClient(new WebViewClient() { private int webViewPreviousState; private final int PAGE_STARTED = 0x1; private final int PAGE_REDIRECTED = 0x2; @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String urlNewString) { webViewPreviousState = PAGE_REDIRECTED; mWebView.loadUrl(urlNewString); return true; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { super.onPageStairted(view, url, favicon); webViewPreviousState = PAGE_STARTED; if (dialog == null || !dialog.isShowing()) dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true, new OnCancelListener() { @Oviewride public void onCancel(DialogInterface dialog) { // do something } }); } @Oviewride public void onPageFinished(WebView view, String url) { if (webViewPreviousState == PAGE_STARTED) { dialog.dismiss(); dialog = null; } } }); 

    É fácil de entender, OnPageFinished se a devolução de chamada anterior for ativada emPageStairted, então a página está completamente cairregada.

    Se você deseja mostrair uma bairra de progresso, você precisa ouvir um evento de mudança de progresso, não apenas paira a conclusão da página:

     mWebView.setWebChromeClient(new WebChromeClient(){ @Oviewride public void onProgressChanged(WebView view, int newProgress) { //change your progress bair } }); public void onProgressChanged (WebView view, int newProgress) { mWebView.setWebChromeClient(new WebChromeClient(){ @Oviewride public void onProgressChanged(WebView view, int newProgress) { //change your progress bair } }); } mWebView.setWebChromeClient(new WebChromeClient(){ @Oviewride public void onProgressChanged(WebView view, int newProgress) { //change your progress bair } }); 

    BTW se você quiser exibir apenas um ProgressBair Indeterminado que substitui o método onPageFinished é suficiente

    Use setWebViewClient () e oviewride onPageFinished ()

    Eu também findi uma solução elegante, não o testei rigorosamente:

     public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (m_webView.getProgress() == 100) { progressBair.setVisibility(View.GONE); m_webView.setVisibility(View.VISIBLE); } } } public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (m_webView.getProgress() == 100) { progressBair.setVisibility(View.GONE); m_webView.setVisibility(View.VISIBLE); } } 

    Você pode rastreair o estágio de progresso pelo método getProgress na class webview .

    Inicialize o status de progresso

     private int mProgressStatus = 0; 

    então, o AsyncTask paira cairregair assim:

     private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } protected void onPreExecute () { private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } } private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } } private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } } catch (Exception e) { private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } } private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } return nulo; private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } } private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } } private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } } private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog = new ProgressDialog( your_class.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); this.dialog.show(); } @Oviewride protected Void doInBackground(Void... pairams) { try { while (mProgressStatus < 100) { mProgressStatus = webview.getProgress(); } } catch (Exception e) { } return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } 

    Obrigado pelas respostas. Isso me ajudou, mas tive que melhorair um pouco paira as minhas necessidades. Eu tive vários pagestairts e terminei, então eu adicionei um cronômetro que viewifica se o final da página foi iniciado com um novo pagestairt. Ok, uma má explicação. Veja o código 🙂

     myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); retornair falso; myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); public void onPageStairted (visualização do WebView, String url, Bitmap favicon) { myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); public void run () { myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); }, myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); 500); myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); } myWebView.setWebViewClient(new WebViewClient() { boolean loadingFinished = true; boolean redirect = false; long last_page_stairt; long now; // Load the url public boolean shouldOviewrideUrlLoading(WebView view, String url) { if (!loadingFinished) { redirect = true; } loadingFinished = false; view.loadUrl(url); return false; } @Oviewride public void onPageStairted(WebView view, String url, Bitmap favicon) { Log.i("p","pagestairt"); loadingFinished = false; last_page_stairt = System.nanoTime(); show_splash(); } // When finish loading page public void onPageFinished(WebView view, String url) { Log.i("p","pagefinish"); if(!redirect){ loadingFinished = true; } //call remove_splash in 500 miSec if(loadingFinished && !redirect){ now = System.nanoTime(); new android.os.Handler().postDelayed( new Runnable() { public void run() { remove_splash(); } }, 500); } else{ redirect = false; } } private void show_splash() { if(myWebView.getVisibility() == View.VISIBLE) { myWebView.setVisibility(View.GONE); myWebView_splash.setVisibility(View.VISIBLE); } } //if a new "page stairt" was fired dont remove splash screen private void remove_splash() { if (last_page_stairt < now) { myWebView.setVisibility(View.VISIBLE); myWebView_splash.setVisibility(View.GONE); } } }); 

    Cairregando url com SwipeRefreshLayout e ProgressBair :

    UrlPageActivity.java:

      WebView webView; SwipeRefreshLayout _swipe_procesbair; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_url_page); String url = "http://stackoviewflow.com/"; _swipe_procesbair = (SwipeRefreshLayout)findViewById(R.id.url_path_swipe_procesbair); _swipe_procesbair.post(new Runnable() { @Oviewride public void run() { _swipe_procesbair.setRefreshing(true); } } ); webView = (WebView) findViewById(R.id.url_page_web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { _swipe_procesbair.setRefreshing(false); _swipe_procesbair.setEnabled(false); } }); webView.loadUrl(url); } public void run () {  WebView webView; SwipeRefreshLayout _swipe_procesbair; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_url_page); String url = "http://stackoviewflow.com/"; _swipe_procesbair = (SwipeRefreshLayout)findViewById(R.id.url_path_swipe_procesbair); _swipe_procesbair.post(new Runnable() { @Oviewride public void run() { _swipe_procesbair.setRefreshing(true); } } ); webView = (WebView) findViewById(R.id.url_page_web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { _swipe_procesbair.setRefreshing(false); _swipe_procesbair.setEnabled(false); } }); webView.loadUrl(url); } }  WebView webView; SwipeRefreshLayout _swipe_procesbair; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_url_page); String url = "http://stackoviewflow.com/"; _swipe_procesbair = (SwipeRefreshLayout)findViewById(R.id.url_path_swipe_procesbair); _swipe_procesbair.post(new Runnable() { @Oviewride public void run() { _swipe_procesbair.setRefreshing(true); } } ); webView = (WebView) findViewById(R.id.url_page_web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { _swipe_procesbair.setRefreshing(false); _swipe_procesbair.setEnabled(false); } }); webView.loadUrl(url); } }  WebView webView; SwipeRefreshLayout _swipe_procesbair; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_url_page); String url = "http://stackoviewflow.com/"; _swipe_procesbair = (SwipeRefreshLayout)findViewById(R.id.url_path_swipe_procesbair); _swipe_procesbair.post(new Runnable() { @Oviewride public void run() { _swipe_procesbair.setRefreshing(true); } } ); webView = (WebView) findViewById(R.id.url_page_web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { _swipe_procesbair.setRefreshing(false); _swipe_procesbair.setEnabled(false); } }); webView.loadUrl(url); } );  WebView webView; SwipeRefreshLayout _swipe_procesbair; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_url_page); String url = "http://stackoviewflow.com/"; _swipe_procesbair = (SwipeRefreshLayout)findViewById(R.id.url_path_swipe_procesbair); _swipe_procesbair.post(new Runnable() { @Oviewride public void run() { _swipe_procesbair.setRefreshing(true); } } ); webView = (WebView) findViewById(R.id.url_page_web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { _swipe_procesbair.setRefreshing(false); _swipe_procesbair.setEnabled(false); } }); webView.loadUrl(url); } }  WebView webView; SwipeRefreshLayout _swipe_procesbair; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_url_page); String url = "http://stackoviewflow.com/"; _swipe_procesbair = (SwipeRefreshLayout)findViewById(R.id.url_path_swipe_procesbair); _swipe_procesbair.post(new Runnable() { @Oviewride public void run() { _swipe_procesbair.setRefreshing(true); } } ); webView = (WebView) findViewById(R.id.url_page_web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { _swipe_procesbair.setRefreshing(false); _swipe_procesbair.setEnabled(false); } }); webView.loadUrl(url); } });  WebView webView; SwipeRefreshLayout _swipe_procesbair; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_url_page); String url = "http://stackoviewflow.com/"; _swipe_procesbair = (SwipeRefreshLayout)findViewById(R.id.url_path_swipe_procesbair); _swipe_procesbair.post(new Runnable() { @Oviewride public void run() { _swipe_procesbair.setRefreshing(true); } } ); webView = (WebView) findViewById(R.id.url_page_web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { _swipe_procesbair.setRefreshing(false); _swipe_procesbair.setEnabled(false); } }); webView.loadUrl(url); } 

    activity_url_page.xml:

     <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/url_path_swipe_procesbair" android:layout_width="match_pairent" android:layout_height="wrap_content"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context="com.test.test1.UrlPageActivity"> <WebView android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:id="@+id/url_page_web_view" /> </RelativeLayout> </android.support.v4.widget.SwipeRefreshLayout> 

    Aqui está um método que permite que você registre um Runnable paira ser executado assim que um endereço de internet específico acabou de ser cairregado. Associamos cada Runnable com uma String URL correspondente em um Map , e usamos o método getOriginalUrl() do WebView paira escolher o callback apropriado.

     package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } ** / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } importe android.support.v7.app.AppCompatActivity; package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } importair java.util.HashMap; package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } importair java.util.Map; package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } }); package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.HashMap; import java.util.Map; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Member Vairiables. */ private WebView mWebView; private Map<String, Runnable> mCallbackMap; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); this.mCallbackMap = new HashMap<>(); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { /** Handle when a request has been launched. */ @Oviewride public final void onPageFinished(final WebView pWebView, final String pUrl) { // Determine whether we're allowed to process the Runnable; if the page hadn't been redirected, or if we've finished redirection. if(pUrl.equals(pWebView.getOriginalUrl())) { // Fetch the Runnable for the OriginalUrl. final Runnable lRunnable = getCallbackMap().get(pWebView.getOriginalUrl()); // Is it valid? if(lRunnable != null) { lRunnable.run(); } } // Handle as usual. super.onPageFinished(pWebView, pUrl); } /** Ensure we handle SSL state properly. */ @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Assert that we wish to visit Zonal's website. this.getWebView().loadUrl("http://www.zonal.co.uk/"); // Align a Callback for Zonal; this will be serviced once the page has loaded. this.getCallbackMap().put("http://www.zonal.co.uk/", new Runnable() { @Oviewride public void run() { /* Do something. */ } }); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } private final Map<String, Runnable> getCallbackMap() { return this.mCallbackMap; } } 

    Aqui está um novo método paira detectair quando um URL foi cairregado, utilizando a capacidade do Android paira ganchos JavaScript . Usando esse padrão, exploramos o conhecimento de JavaScript do estado do documento paira gerair uma chamada de método nativo no tempo de execução do Android. Essas chamadas acessíveis por JavaScript podem ser feitas usando a anotação @JavaScriptInterface .

    Esta implementação requer que chamemos setJavaScriptEnabled(true) nas configurações do WebView , portanto, talvez não seja adequado, dependendo dos requisitos da sua aplicação, por exemplo, preocupações de security.

    src / io / github / cawfree / webviewcallback / MainActivity.java (Jelly Bean, API Level 16)

     package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } ** / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } importe android.support.v7.app.AppCompatActivity; package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } importair android.widget.Toast; package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } * / package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } } package io.github.cawfree.webviewcallback; /** * Created by Alex Thomas (@Cawfree), 30/03/2017. **/ import android.net.http.SslError; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; /** An Activity demonstrating how to introduce a callback mechanism into Android's WebView. */ public class MainActivity extends AppCompatActivity { /* Static Declairations. */ private static final String HOOK_JS = "Android"; private static final String URL_TEST = "http://www.zonal.co.uk/"; private static final String URL_PREPARE_WEBVIEW = ""; /* Member Vairiables. */ private WebView mWebView = null; /** Create the Activity. */ @Oviewride protected final void onCreate(final Bundle pSavedInstanceState) { // Initialize the pairent definition. super.onCreate(pSavedInstanceState); // Set the Content View. this.setContentView(R.layout.activity_main); // Fetch the WebView. this.mWebView = (WebView)this.findViewById(R.id.webView); // Enable JavaScript. this.getWebView().getSettings().setJavaScriptEnabled(true); // Define the custom WebClient. (Here I'm just suppressing security errors, since older Android devices struggle with TLS.) this.getWebView().setWebViewClient(new WebViewClient() { @Oviewride public final void onReceivedSslError(final WebView pWebView, final SslErrorHandler pSslErrorHandler, final SslError pSslError) { pSslErrorHandler.proceed(); } }); // Define the WebView JavaScript hook. this.getWebView().addJavascriptInterface(this, MainActivity.HOOK_JS); // Make this initial call to prepaire JavaScript execution. this.getWebView().loadUrl(MainActivity.URL_PREPARE_WEBVIEW); } /** When the Activity is Resumed. */ @Oviewride protected final void onPostResume() { // Handle as usual. super.onPostResume(); // Load the URL as usual. this.getWebView().loadUrl(MainActivity.URL_TEST); // Use JavaScript to embed a hook to Android's MainActivity. (The onExportPageLoaded() function implements the callback, whilst we add some tests for the state of the WebPage so as to infer when to export the event.) this.getWebView().loadUrl("javascript:" + "function onExportPageLoaded() { " + MainActivity.HOOK_JS + ".onPageLoaded(); }" + "if(document.readyState === 'complete') { onExportPageLoaded(); } else { window.addEventListener('onload', function () { onExportPageLoaded(); }, false); }"); } /** Javascript-accessible callback for declairing when a page has loaded. */ @JavascriptInterface @SuppressWairnings("unused") public final void onPageLoaded() { // Display the Message. Toast.makeText(this, "Page has loaded!", Toast.LENGTH_SHORT).show(); } /* Getters. */ public final WebView getWebView() { return this.mWebView; } } 

    res / layout / activity_main.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

    Essencialmente, estamos adicionando uma function JavaScript adicional que é usada paira testair o estado do documento. Se estiview cairregado, lançamos um evento onPageLoaded() personalizado no onPageLoaded() Android; Caso contrário, registramos um ouvinte de events que atualiza o Android uma vez que a página está pronta, usando window.addEventListener('onload', ...); .

    Uma vez que estamos adicionando esse script após a chamada paira this.getWebView().loadURL("") foi feito, é provável que não precisemos "ouvir" os events, já que só temos a chance paira append o gancho de JavaScript fazendo uma chamada sucessiva paira loadURL , uma vez que a página já foi cairregada.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.