简体   繁体   English

打开对话框选择浏览器

[英]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. 当用户单击WebView中的链接时,默认行为是加载任何可以处理该链接的默认应用程序。 So if you click on a web URL, the browser will open to handle it. 因此,如果您单击Web URL,浏览器将打开进行处理。 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: 通过将WebView的WebViewClient设置为WebViewClient的新实例,您可以超级快地完成此操作,如下所示:

OnCreate: 在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 检查以下链接以获取有关WebViewClient的更多信息

handling links in a webview 处理Web视图中的链接

but the problem is when i click the links on page loaded in webview it opens in default browser 但是问题是当我单击加载在Webview中的页面上的链接时,它在默认浏览器中打开

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. 然后,Android停止向用户显示选项列表,并继续在选定的默认浏览器中打开链接。

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. 唯一的解决方案是通过进入settings来清除该首选项。然后android会使用List向您显示Complete action。

在此处输入图片说明

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. 而且,如果您想使用复杂的方法,则可以使用Javascript注入并在单击链接时调用Android代码,从而以编程方式向用户显示浏览器列表。

you can use PackageManager and queryIntentActivityOptions() to filter your activity out and get a list of other activities that the user can choose from. 您可以使用PackageManagerqueryIntentActivityOptions()过滤掉您的活动并获取用户可以选择的其他活动的列表。

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

@Neha it depends upon how many browsers are available in your mobile. @Neha,这取决于您的手机中可用的浏览器数量。 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. 如果您下次安装了chrome浏览器,则下次运行您的应用时,系统会要求选择浏览器选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android java上打开选择目录对话框? - How open choose directory dialog on Android java? 提供始终选择浏览器以打开链接的选项 - Give the option to always choose a browser to open a link 在移动设备浏览器中无法打开Facebook请求对话框 - Facebook apprequest dialog not open in mobile device browser 设置标题以选择应用程序以从浏览器打开 pdf - Set header to choose app to open pdf from browser 如何在我的浏览器应用程序中打开默认浏览器应用程序对话框 - How to open Default Browser App dialog in my browser app 在 android 中,如何在不弹出“选择浏览器”列表的情况下强制使用特定浏览器打开 URI? - In android, How can I force open a URI using a specific browser, without popping up the 'choose browser' list? 自定义“选择应用程序”对话框 - Customization of “Choose app” dialog 如何像某些自定义对话框一样将浏览器作为前台活动打开? - How to open browser as a foreground activity like some custom dialog? 有没有办法使用声明样式打开选择资源对话框,让您在 Android Studio 中选择颜色或可绘制对象? - Is there a way to use declare-styleable to open the choose a resource dialog that lets you select a color or a drawable in Android Studio? 对话框打开另一个对话框 - Dialog to open another dialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM