webChromeClient将打开浏览器链接打开浏览器、链接、webChromeClient

2023-09-06 02:16:14 作者:诱惑是上床的关键。

我已经搜索并阅读了大量的帖子,但无法弄清楚如何做到这一点在我的code。

我想用地理位置在我的应用程序,需要查看webChromeClient在webViewClient至极我用现在的HTML文件和链接不会停留在同样的看法代替。

当我更改为webChromeClient的HTML链接,如< A HREF =HTTP://url/file.php Q = 123,是突然打开浏览器!

我怎样才能prevent呢?

  myWebView =新的WebView(本);
myWebView.getSettings()setJavaScriptEnabled(真)。
。myWebView.getSettings()setLoadWithOverviewMode(真正的);
。myWebView.getSettings()setUseWideViewPort(真正的);
myWebView.getSettings()setGeolocationEnabled(真)。
myWebView.setWebChromeClient(新WebChromeClient(){
公共无效onGeolocationPermissionsShowPrompt(字符串出身,android.webkit.GeolocationPermissions.Callback回调){
        callback.invoke(原产地,真,假); }
});
myWebView.loadUrl(文件:///android_asset/HTML/index.html);
的setContentView(myWebView);
 

解决方案

WebChromeClient不包含shouldOverrideUrlLoading方法,所述WebViewClient一样。还记得的WebView可以而且确实在同一时间同时使用WebViewClient和WebChromeClient如果指定。该WebViewClient增加了不提供给你的方法,没有分配(保持导航在web视图)客户端。与WebChromeClient同样有它可以使用特定的方法(GET负载例如网页标题)。

所以,你可以建立自己的code是这样的:

 的WebView网页=(web视图)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(真正的);
webSettings.setGeolocationEnabled(真正的);
webSettings.setSupportMultipleWindows(真正的); //这迫使ChromeClient启用。

web.setWebChromeClient(新WebChromeClient(){
    @覆盖
    公共无效onReceivedTitle(web视图查看,串题){
         getWindow()的setTitle(职称)。 //设置活动平铺页面标题。
    }
});

web.setWebViewClient(新WebViewClient(){
    @覆盖
    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
        view.loadUrl(URL);
        返回false;
    }
});
 
电脑浏览器中所有链接都打不开

I have searched and read a lot of posts but can not figure out how to do it in my code.

I want to use geolocation in my app and need to view in webChromeClient in stead of webViewClient wich I use for the html files now and the links does stay in the same view.

When I change this to webChromeClient, the html links, like <a href="http://url/file.php?q=123", are suddenly opening in the browser!

How can I prevent this?

myWebView = new WebView(this);  
myWebView.getSettings().setJavaScriptEnabled(true);   
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setGeolocationEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) { 
        callback.invoke(origin, true, false); }
});
myWebView.loadUrl("file:///android_asset/HTML/index.html");
setContentView(myWebView);

解决方案

WebChromeClient doesn't contain the shouldOverrideUrlLoading method, the WebViewClient does. Remember the "WebView" can and does use both WebViewClient and WebChromeClient at the same time if specified. The WebViewClient adds methods not available to you with no client assigned (keeping navigation in the webview). The same with the WebChromeClient has specific methods it can use (get page title on load for example).

So you can build your code like this:

WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.    

web.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onReceivedTitle(WebView view, String title) {
         getWindow().setTitle(title); //Set Activity tile to page title.
    }
});

web.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});