php - android webview 内的链接被打开两次

标签 php android html mysql android-webview

我在使用 android 的 webview 时遇到了一个奇怪的问题。我将一个页面加载到 WebView 中,其中有一个链接。我已将 Web View 设置为在同一 Web View 中(而不是在浏览器上)打开该链接。当打开下一页时,它会向 mysql 写入一些内容。如果用户已经在同一天打开了第二个页面,则用户将被重定向到第一页,并且不会将任何内容写入 mysql。我注意到,在第一种情况下,按链接的 3-4 次中有 1 次会将内容写入 mysql 两次(具有相同的时间戳),并将用户立即重定向到首页。所以看起来当点击链接时,url 会连续打开两次。在我的计算机浏览器(safari、firefox 和 chrome)上,一切都按预期运行。我发现一些文章说图像上的空 src 字段可能会导致此问题,但我的 php/html 代码中没有空 src 字段。

Webview设置如下:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);

        WebViewClient yourWebClient = new WebViewClient()
        {

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                webView.loadUrl("file:///android_asset/404.html");
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String  url)
            {
             if ( url.contains("google") == false ) {                   
                 webView.loadUrl(url);
                 return false;
             }
             else {
                 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                 startActivity(i);
                 return true;
             }               
            }
        };

        webView = (WebView) findViewById(R.id.description);
        webView.getSettings().setJavaScriptEnabled(true);

        if (Build.VERSION.SDK_INT >= 11){
            webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }

        String url = (MainActivity.URL+"app/description.php?tagid="+tagId+"&screenwidth="+viewPortWidth+"&screenheight="+viewPortHeight+"&deviceid="+MainActivity.myDeviceId+"&lang="+MainActivity.myLanguage+"&os=android);

        webView.setWebViewClient(yourWebClient);
        webView.loadUrl(url);
    }

我的链接只是 anchor 标记内的典型 div(用 php 编写):

echo '<a href="description.php?type='.$type.'&screenwidth='.$screenWidth.'&screenheight='.$screenHeight.'&tagid='.$tagid.'&no=1&deviceid='.$deviceid.'&lang='.$language.'">
<div class="redbutton">'.$lang['NO'].'</div></a>';

最佳答案

实际上只要返回false,网页就会自动加载到WebView中。无需使用webView.loadUrl();

if ( url.contains("google") == false ) {                   
                 return false;
             }

尝试一下,如果不起作用请告诉我。

Documentation州 -

while return false means the current WebView handles the url

关于php - android webview 内的链接被打开两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26144244/

相关文章:

php - PHP-构建搜索引擎(查询多个数据库)

java - 通过类名动态实例化 Fragment

jquery - 选择第一,第二。 . . div 在 jquery while 循环中

php - 使用PHP从android连接mysql

php - html 表单预设值字段

android - Android 4.3 上的 "requestLayout() improperly called by..."错误

android - 添加android支持设计库后Gradle报错

html - CSS - 边框和背景颜色之间的空间

html - 使宽表适合 Bootstrap 容器

php - 如何使用 PHP 在我的安全站点上显示 protected Amazon S3 图像?