[英]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的更多信息
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. 您可以使用
PackageManager
和queryIntentActivityOptions()
过滤掉您的活动并获取用户可以选择的其他活动的列表。
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.