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 genericnew WebViewClient()to your WebView instead of your ownMyWebViewClientclass. 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. ThegetHost()method returns just the domain (likewww.ieltsmadeeasy.tk) without thehttp://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
- Assign the custom WebViewClient: We replaced the generic
WebViewClientwith yourMyWebViewClientso your link-handling rules are actually applied. - Fixed host validation: Now we check if the host exactly matches your domain (
www.ieltsmadeeasy.tk)—no extrahttp://or slashes, which aligns with whatgetHost()returns. - Added API 24+ support: The newer
shouldOverrideUrlLoadingoverload is implemented to ensure consistent behavior across all modern Android versions. - 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
truewithout launching the intent.
内容的提问来源于stack exchange,提问作者Robin Chauhan
相关产品推荐
相关产品推荐

