Detect URL Webview

Asked

Viewed 489 times

4

I have a little problem, I need to know the URL every time the page is changed.

my code works perfectly normally opens youtube that would be the example :

  WebView wv;

    public void onBackPressed(){
        if(wv.canGoBack()){
            wv.goBack();
        }else{
            super.onBackPressed();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wv = (WebView) findViewById(R.id.wv);
        //Enable Javascript
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setFocusableInTouchMode(true);
        // set Render Priority to high
        wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        wv.getSettings().setDomStorageEnabled(true);
        wv.getSettings().setAppCacheEnabled(true);
        wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        // Load url
        wv.loadUrl("https://www.youtube.com/");
        wv.setWebViewClient(new WebViewClient());
    }

I looked for solutions on foreign sites for my problem and found this one

WebView wv;

public void onBackPressed(){
    if(wv.canGoBack()){
        wv.goBack();
    }else{
        super.onBackPressed();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wv = (WebView) findViewById(R.id.wv);
    //Enable Javascript
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setFocusableInTouchMode(true);
    // set Render Priority to high
    wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    wv.getSettings().setDomStorageEnabled(true);
    wv.getSettings().setAppCacheEnabled(true);
    wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    // Load url
    wv.loadUrl("https://www.youtube.com/");
    wv.setWebViewClient(new myWebViewClient());
}

String currentUrl;

private class myWebViewClient extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading (WebView view, String url){
        Log.e("URL Now",url);
        currentUrl=url;
        view.loadUrl(url); // se eu usar isso, já não fica em branco a inicial, mas msm assim não alerta qndo muda de URL
        return true;
    }
}

I checked the logs and saw that appeared the URL as soon as opened the APP, but did not open youtube, the screen is all white.

My first example works perfectly, but does not meet my needs.

My goal is to know is to detect when I click on a video, so I need to know.

Is there any other more elegant way to do that ?

2 answers

4


Good evening Bruno, I think I discovered + or - what you want, the data that is caught you need to filter with regex, will depend a lot on what you want, because it appears all Source, but precisely, change the page, changes in the log.

package com.example.offboard.youtubedownload;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    WebView wv;

    public void onBackPressed(){
        if(wv.canGoBack()){
            wv.goBack();
        }else{
            super.onBackPressed();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wv = (WebView) findViewById(R.id.wv);
        //Enable Javascript
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setFocusableInTouchMode(true);
        // set Render Priority to high
        wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        wv.getSettings().setDomStorageEnabled(true);
        wv.getSettings().setAppCacheEnabled(true);
        wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        // Load url
        wv.loadUrl("https://www.youtube.com/");
        wv.setWebViewClient(new myWebViewClient());
    }

     String currentUrl;

    private class myWebViewClient extends WebViewClient{

        @Override
        public void onLoadResource(WebView view, String url) {
            Log.e("Resource ",url); // talvez você precise usar regex para filtrar a URL, mas foi o único jeito que achei
        }

    }

}
  • 1

    I’ll open the project here and test it now before I go to the gym.

  • Poxa cara worked perfectly, thanks, but it takes all same kkkk, I turn with the regex to filter the page, thanks even.

2

This question has already been accepted but I will put here some options of methods of WebViewClient that may be useful.

onPageStarted - It is called to inform that a page has started to load.

void onPageStarted(WebView view, String url, Bitmap favicon);

onPageFinished - It is called to inform that a page has finished loading.

void onPageFinished(WebView view, String url);

onLoadResource - It is called every time a resource will start to load.

void onLoadResource(WebView view, String url);

shouldOverrideUrlLoading-(Deprecated, New) - Call before loading a page, gives the programmer a chance to control if the page can load. On returning true you abort loading, returning false loading and permitted.

boolean shouldOverrideUrlLoading(WebView view, String url);*
boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request);

*This method was deprecated in API level 24

shouldInterceptRequest-(Deprecated, New) - Intercepts the loading of a resource, giving the programmer a chance to change the resource, if it returns null the resource load will remain normal.

WebResourceResponse shouldInterceptRequest (WebView view, String url)*
WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request);

*This method was deprecated in API level 21


Example of implementation:

WebViewClient mWebViewClient = new WebViewClient(){

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon); /// executa o padrão
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url); /// executa o padrão
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url); /// executa o padrão
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request); /// executa o padrão
    }

    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        return super.shouldInterceptRequest(view, request); /// executa o padrão
    }
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.