You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android WebView全站浏览问题求助:禁止跳转默认浏览器

Hey there! Let's fix this issue so all links stay within your WebView instead of jumping to the system browser. The problem comes from two main points in your current code—let's break it down and adjust things step by step.

What's Wrong in the Current Code?

  • You're not using your custom MyWebViewClient: Right now, you're setting a generic new WebViewClient() to your WebView instead of your own MyWebViewClient class. That means your custom link-handling logic isn't even running!
  • Incorrect host matching: The condition Uri.parse(url).getHost().endsWith("http://www.ieltsmadeeasy.tk/") is flawed. The getHost() method returns just the domain (like www.ieltsmadeeasy.tk) without the http:// prefix or trailing slash, so this check will never be true. As a result, all links are being sent to the system browser.

Fixed Code Implementation

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.Nullable;
import java.util.Objects;

public class MainActivity extends Activity { 
    private WebView mWebView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        mWebView = findViewById(R.id.activity_main_webview); 

        // Use your custom MyWebViewClient instead of the default one
        mWebView.setWebViewClient(new MyWebViewClient()); 

        WebSettings webSettings = mWebView.getSettings(); 
        webSettings.setJavaScriptEnabled(true); 
        mWebView.loadUrl("http://www.ieltsmadeeasy.tk/"); // Load your full base URL
    } 

    @Override 
    public void onBackPressed() { 
        if(mWebView.canGoBack()) { 
            mWebView.goBack(); 
        } else { 
            super.onBackPressed(); 
        } 
    } 

    // Keep this inner class accessible to MainActivity
    private class MyWebViewClient extends WebViewClient { 
        // Handle links for older Android versions (pre-API 24)
        @Override 
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            Uri uri = Uri.parse(url);
            String host = uri.getHost();

            // Check if the link belongs to your domain
            if (host != null && host.equals("www.ieltsmadeeasy.tk")) { 
                // Let WebView handle this link internally
                return false; 
            } 
            // Optional: Uncomment below to open external links in browser, or remove to block them
            // Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
            // view.getContext().startActivity(intent); 
            return true; 
        } 

        // Handle links for API 24 and above (covers modern Android versions)
        @Override 
        public boolean shouldOverrideUrlLoading(WebView view, android.webkit.WebResourceRequest request) { 
            Uri uri = request.getUrl();
            String host = uri.getHost();

            if (host != null && host.equals("www.ieltsmadeeasy.tk")) { 
                return false; 
            } 
            // Optional: Handle external links if needed
            // Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
            // view.getContext().startActivity(intent); 
            return true; 
        } 
    }
}

Key Changes Explained

  1. Assign the custom WebViewClient: We replaced the generic WebViewClient with your MyWebViewClient so your link-handling rules are actually applied.
  2. Fixed host validation: Now we check if the host exactly matches your domain (www.ieltsmadeeasy.tk)—no extra http:// or slashes, which aligns with what getHost() returns.
  3. Added API 24+ support: The newer shouldOverrideUrlLoading overload is implemented to ensure consistent behavior across all modern Android versions.
  4. Flexible external link handling: If you want external links to open in the browser, uncomment the intent lines. If you prefer to block them entirely, just return true without launching the intent.

内容的提问来源于stack exchange,提问作者Robin Chauhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:31:18