简体   繁体   中英

Is it possible to open only one specific link within a browser within a WebView android app?

I've been trying to dabble in Android apps recently, and I created a simple web view app that opens my website. Everything works as intended, and I was able to get the links to open within the app itself from the code below.

    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        return false;
    }

I was curious if I could only open a specific link in the browser, however?

I attempted to use

if (url.equals("my url here")) {
          //
}

inside of the method I provided above, but I couldn't figure out what to call inside my if statement. I've tried to look online a bit, but most of the methods I've found seem to be deprecated.

I want the URL I define to open within the default browser, but everything else is opened within the app.

Thanks for any help you may give!

Edit: WebViewClientImpl class

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewClientImpl extends WebViewClient {

    private Activity activity = null;
    private android.content.Context Context;

    public WebViewClientImpl(Activity activity) {
        this.activity = activity;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if (url.equals("my url here")) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

            return true;
        }
        return false;
    }
}

Yes, that's quite possible. Something like this should do it:

public boolean shouldOverrideUrlLoading(WebView webView, String url) {
    if (url.equals("my url here")) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);

        return true;
    }

    return false;
}

Note that you'll return true in the case of loading the URL via the browser to cancel loading it in your WebView.

Try this:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewClientImpl extends WebViewClient {

   Context context;

    public WebViewClientImpl(Context context1) { //pass activity here 
        this.context= context1;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if (url.equals("my url here")) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            context.startActivity(i);

            return true;
        }
        return false;
    }
}

If you are still in confusion just put this class into activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM