Android WebView click open within WebView not a default browser

后端 未结 12 1930
迷失自我
迷失自我 2020-11-27 16:34

I did one sample application using WebView, in that web view the URL comes from web services. It\'s working fine, but if I click any link within that WebView, its automatica

相关标签:
12条回答
  • 2020-11-27 17:00

    I face same problem and i just fixed it by adding single line.

    webview.setWebViewClient(new WebViewClient());
    

    and this solved my problem.

    0 讨论(0)
  • 2020-11-27 17:02

    Here is my code how to resolve the above problem (when cliking on link it asking for default browser to opn the link)

    import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;
    import android.view.Menu;import android.webkit.WebChromeClient;
    import android.webkit.WebView;public class MainActivity extends Activity{
    `@SuppressLint("SetJavaScriptEnabled")@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String url="http://google.com";WebView Webview=(WebView) this.findViewById(R.id.webView1);     Webview.getSettings().setJavaScriptEnabled(true);Webview.loadUrl(url);}@Override    public boolean onCreateOptionsMenu(Menu menu){//Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);return true;}`}
    
    0 讨论(0)
  • 2020-11-27 17:02

    Most of the answer is right but please notice that: SupportMultipleWindows must be set as false.

    mWebView.getSettings().setSupportMultipleWindows(false);
    

    and now set webViewClint and get loading URL.

    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
    
        Log.d(TAG,"URL: "+url);
        view.loadUrl(url);
    
        /**
         * if you wanna open outside of app
         if (url != null && url.startsWith(URL)) {
         view.loadUrl(url);
         return false;
         }
         // Otherwise, give the default behavior (open in browser)
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
         startActivity(intent);**/
        return true;
    
    }
    
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }
    
    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
    }
    
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    
        }
    });
    
    0 讨论(0)
  • 2020-11-27 17:03
    webview.setWebViewClient(new WebViewClient());
    webview.loadUrl(url);
    

    Do set webview client.

    0 讨论(0)
  • 2020-11-27 17:04

    You need to set up a WebViewClient in order to override that behavior (opening links using the web browser). You obviously have your WebView declared, but then set up a WebViewClient like so:

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient());
    

    Then you need to define your WebViewClient():

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.example.com")) {
                // Designate Urls that you want to load in WebView still.
                return false;
            }
    
            // Otherwise, give the default behavior (open in browser)
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
    

    Then start your WebViewClient:

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new MyWebViewClient());
    

    http://developer.android.com/guide/webapps/webview.html

    0 讨论(0)
  • 2020-11-27 17:07

    I did like this and its working perfect..

    mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != null && url.startsWith("http://")) {
                    mWebView.loadUrl(url);
                    return true;
                } else {
                    return false;
                }
            }
        });
    
    0 讨论(0)
提交回复
热议问题