简体   繁体   中英

Open dialog to choose browser

我有一个加载了url的webview,其中有一些URL可单击,但是问题是当我单击加载在webview中的页面上的链接时,它在默认浏览器中打开,我想在单击时打开包含所有浏览器的对话框在设备上的链接上,要求选择浏览器,并且应该在选定的浏览器中打开它?

Overriding the Default Click Behavior

when the user clicks on a link in the WebView the default behavior is to load whatever default app can handle the link. So if you click on a web URL, the browser will open to handle it. If you were trying to navigate between locally built web pages, you'd need to override this functionality. Luckily this is not difficult to handle. You can do it super quick by setting the WebViewClient of your WebView to a new instance of WebViewClient like so:

OnCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView webView = (WebView) findViewById(R.id.webView1);

    WebViewClient viewClient = new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url)); 
            startActivity(intent); 
            view.loadUrl(url);
            return true;

        }
    };

    webView.setWebViewClient(viewClient);
    webView.loadUrl("http://www.google.com");
}

Check the below link for more info on WebViewClient

handling links in a webview

but the problem is when i click the links on page loaded in webview it opens in default browser

If you have selected

use by default this application checkbox

Then the android stops showing options list to user and continues opening link in selected default browser.

The only solution you can have is clear that preference by going into settings.And then onwards android will show you Complete action using List.

在此处输入图片说明

And if you want to go by complex way then you can use Javascript injection and invoke Android code when link is clicked which will programatically display browser list to user.

you can use PackageManager and queryIntentActivityOptions() to filter your activity out and get a list of other activities that the user can choose from.

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

@Neha it depends upon how many browsers are available in your mobile. for eg,if u have only default browser it redirects to that browser only. if u installed chrome browser next time run your app it ask choose browser option.

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