死循环Android中的WebView backkey的重定向HREF链接重定向、链接、WebView、Android

2023-09-06 04:36:33 作者:云深不知处

我要在Android中的WebView应用程序中的死循环与backkey在处理重定向链接。

例如,当我的web视图开始,它进入link0。

在link0,存在链接至链路1 A HREF链路。链接1 redrect到链接2

所以,如果我点击链接1,它会去链接1,然后重定向到链接2。当我点击backkey,它应该回到link0,在我的情况。但是,相反,它进入LINK1,它重定向回链接2了。所以,我永远没有机会回去了。

在backkey正常工作与其他链接,如果他们不重定向连接。

我GOOGLE网求助,但没有发现相关的问题。

顺便说一句,在backkey工作在互联网浏览器如预期。但不是在web视图。

下面是我的code,你可以试试。正如你可以在code看,我都尝试onBack pressed和的onkeydown,但neigher作品。

感谢你的帮助。我已经struglling在这一段时间。

=============================================== ===================================

 公共类MyActivity扩展活动
{
    私人的WebView myWebView;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        myWebView =(web视图)findViewById(R.id.webview);
        myWebView.getSettings()setJavaScriptEnabled(真)。
        myWebView.loadUrl(http://50.112.242.120/temp/);
        myWebView.setWebViewClient(新MyWebViewClient());
    }

    私有类MyWebViewClient扩展WebViewClient {
        @覆盖
        公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
            myWebView.loadUrl(URL);
            返回true;
        }
    }

    @覆盖
    公共布尔的onkeydown(INT键code,KeyEvent的事件){
        //检查关键事件是后退按钮,如果有历史记录
        如果((钥匙code == KeyEvent.KEY code_BACK)及和放大器; myWebView.canGoBack()){
            myWebView.goBack();
            返回true;
        }
        //如果不是返回键或没有网页的历史,泡到默认
        //系统行为(可能退出活动)
        返回super.onKeyDown(键code,事件);
    }
}
 
谷歌为Android TV开发者提供多种新功能

  // main.xml中
< XML版本=1.0编码=UTF-8&GT?;
<的WebView的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / web视图
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
/>
 

解决方案

我有一个同样的问题,解决了它。 这里是我的回答。

当我点击一个链接(www.new.a),它会自动重定向其他链接(mobile.new.a)。通常情况下,链接重定向两个或三个,和我的解决方案已经制定上几乎每​​一个重定向的链接。我希望这个答案帮助您与annyoing重定向连接。

我终于想通了这一点。你需要有四个API的WebViewClient。有shouldOverrideUrlLoading(),onPageStarted(),onPageFinished(),和doUpdateVisitedHistory()中的WebViewClient。您需要的所有的API是API 1,因此不用担心。

这里是我的回答。检查出的! :)

I got to a dead loop in Android webview application with backkey while dealing with redirect links.

For example, when my webview started, it goes to link0.

In link0, there is a href link which link to link1. Link1 redrect to link2.

So if I click link1, it will go to link1, then redirect to link2. When I click backkey, it should go back to link0, in my case. But instead, it goes to link1, which redirect back to link2 again. So I never have a chance to go back.

The backkey works correctly with other links if they are not redirect links.

I googled webs for help, but didn't find related question.

By the way, the backkey works in the internet browser as expected. But not in webview.

Below is my code, for you to try. As you can see in the code, I tried both onBackPressed and onKeyDown, but neigher works.

Thanks for your kind help. I have struglling on this for a while.

==================================================================================

public class MyActivity extends Activity 
{
    private WebView myWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("http://50.112.242.120/temp/");
        myWebView.setWebViewClient(new MyWebViewClient());
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            myWebView.loadUrl(url);
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
}

.

// main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

解决方案

I had a same problem and solved it. Here is my answer.

When I click the first link(www.new.a) it automatically redirects other link(mobile.new.a). Usually the links redirect two or three, and my solution have been worked on almost every redirect links. I hope this answer help you out with annyoing redirecting links.

I finally figured out that. You need a WebViewClient with four APIs. There are shouldOverrideUrlLoading(), onPageStarted(), onPageFinished(), and doUpdateVisitedHistory() in the WebViewClient. All the APIs you need is API 1 so don't worry about.

Here is my answer. Check out that! :)