机器人的WebView无视目标=" _blank"当加WebViewClient机器人、目标、WebView、WebViewClient

2023-09-06 13:53:03 作者:娇软的酷

我面临着一个奇怪的问题。

在我的应用程序,我需要的基础上,从资产的文件夹中的WebView点击按钮加载一个静态的HTML文件。

现在在5个HTML文件,一个HTML文件包含15静态链接。这些链接需要重定向用户到所提到的网址在手机浏览器。我已经使用目标=_空白为目的,遵循我的HTML文件。

 < D​​IV CLASS =盖子>< A HREF =htt​​ps://www.irctc.co.in/目标=_空白>铁路预订< / A>< / DIV>
 
科技浪潮来袭 谁将成为湛江的 智 高点

现在,这个工作正常,在一个简单的的WebView一个示例应用程序无任何 WebViewClient 添加到它。

但我需要一个 WebViewClient 我的其他功能。所以在那个时候 目标=_空白 是完全忽略。并且URL是在一个web视图本身打开。

我找到了一个解决办法,我可以使用 shouldOverrideUrlLoading 如下:

  myWebView.setWebViewClient(新WebViewClient(){
    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
    // TODO自动生成方法存根

        如果(url.equalsIgnoreCase(https://www.irctc.co.in/))
        {
            view.getContext()。startActivity(
                            新的意图(Intent.ACTION_VIEW,Uri.parse(URL)));
            返回true;
        }
        其他
            返回super.shouldOverrideUrlLoading(查看,网址);
    }
});
 

因此​​,这是开放的特定的URL在默认浏览器。

所以基本上我的问题是:

为什么目标=_空白忽略,而我们使用 WebViewClient ?而且还有没有其他的解决此问题?因为我有15个链接,我需要比较。我无法加载所有URL在新的浏览器,因为有几个环节,这就需要在同一个web视图中打开了。

解决方案

我有一个想法,一个解决方法。更换应在新窗口中打开一个自定义模式中的所有环节。然后你就可以处理你自己的。

对于一个最小的中断也设置一个自定义主题,并处理所有​​配置更改:

 <活动机器人:名称=com.example.LinkHandler
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
          机器人:主题=@安卓风格/ Theme.Translucent>
    <意向滤光器>
        <作用机器人:名称=android.intent.action.VIEW/>

        <类机器人:名称=android.intent.category.DEFAULT/>
        <类机器人:名称=android.intent.category.BROWSABLE/>

        <数据机器人:计划=your.link.handler.schema/>
    &所述; /意图滤光器>
< /活性GT;
 

然后在您的链路处理器可以使用 getIntent()读取URL。的getData()

也请记住,你应该处理HTTP和HTTPS,我跳,在我短暂例如上面。

下面是一个JavaScript示例如何重写的网址:

的(VAR I = 0; I< document.links.length;我++) {     如果(document.links [I] .TARGET ==_blank){         document.links [I] .href =。your.schema.for+ document.links [I] .href;     } }

< UL>     <李>< A HREF =htt​​p://example.com>试验1< / A>< /李>     <李>< A HREF =htt​​p://example.net目标=_空白>测试2版; / A>< /李>     <李>< A HREF =htt​​ps://example.com目标=_空白>测试第3版; / A>< /李>     <李>< A HREF =htt​​ps://example.net>测试4℃; / A>< /李> < / UL>

I am facing a strange issue.

In my application, I need to load a static html file based on clicked button in a WebView from assets folder.

Now among 5 html files, one html file contains 15 static links. These links need to redirect user to the mentioned url in a mobile browser. I have used target="_blank" for that purpose as follows in my html file.

<div class="lid"><a href="https://www.irctc.co.in/" target="_blank">Railway Reservation </a></div>

Now, this works fine in a sample application with a simple WebView without any WebViewClient added to it.

But I need a WebViewClient for my other functionalities. So at that time target="_blank" is totally ignored. And the url is opened in a WebView itself.

I found a workaround, that I can use shouldOverrideUrlLoading as follows:

myWebView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub

        if(url.equalsIgnoreCase("https://www.irctc.co.in/"))
        {
            view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }
        else
            return super.shouldOverrideUrlLoading(view, url);
    }
});

So this is opening that particular url in a default browser.

So basically my questions are:

Why is target="_blank" ignored while we use WebViewClient? And is there any other workaround for this issue? Because I have 15 links, which I would need to compare. I can't load all urls in a new browser, as there are a few links, which need to be opened in a same WebView, too.

解决方案

I have an idea for a workaround. Replace all links which should been opened in a new window with a custom schema. Then you can handle that by your own.

For a minimal interruption set also a custom theme and handle all configuration changes:

<activity android:name="com.example.LinkHandler"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
          android:theme="@android:style/Theme.Translucent">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>

        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>

        <data android:scheme="your.link.handler.schema"/>
    </intent-filter>
</activity>

Then in your link handler you can read the url by using getIntent().getData().

Please also keep in mind that you should handle http and https, I skipped that in my short example above.

Here is an JavaScript example how to rewrite the urls:

for(var i=0; i<document.links.length; i++) {
    if(document.links[i].target == "_blank") {
        document.links[i].href = "your.schema.for."+document.links[i].href;
    }
}

<ul>
    <li><a href="http://example.com">Test 1</a></li>
    <li><a href="http://example.net" target="_blank">Test 2</a></li>
    <li><a href="https://example.com" target="_blank">Test 3</a></li>
    <li><a href="https://example.net">Test 4</a></li>
</ul>