获取的WebView导航事件 [英] Get the WebView navigation event

查看:168
本文介绍了获取的WebView导航事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序,可以张贴鸣叫叽叽喳喳的工作,我用Web View部件做。如果用户没有登录,它会去登录屏幕,如果它在它登录的用户会去推文。我的要求是从我的应用程序twetting后,它应该回我的应用程序即可。我怎么可以处理的WebView这种情况。我将如何从我的WebView得到重定向URL。

请帮我。

请看看我的code:

 公共类TestTwittershareActivity延伸活动{
    的WebView的WebView;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        网页流量=(的WebView)findViewById(R.id.webview);
        webview.getSettings()setJavaScriptEnabled(真)。
        webview.loadUrl(http://www.twitter.com?status=);
        webview.setWebViewClient(新HelloWebViewClient());
    }    公共布尔的onkeydown(INT键code,KeyEvent的事件){
        如果((键code == KeyEvent.KEY code_BACK)及和放大器; webview.canGoBack()){
            webview.goBack();
            返回true;
        }
        返回super.onKeyDown(键code,事件);
    }    私有类HelloWebViewClient扩展WebViewClient {
        @覆盖
        公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
            view.loadUrl(URL);            返回true;
        }
    }
}


解决方案

如果我正确地理解你的问题,你需要一种方法来检查用户是否有送他的鸣叫完成。最好的办法是检查在您的WebView加载的URL。我们希望,这个网址有某种的鸣叫完成指示(状态部分或许真的?)。查询的网址,您可以使用您已经创建的HelloWebViewClient类并重写它的onPageFinished方法。例如是这样的:

  @覆盖
公共无效onPageFinished(的WebView视图,字符串URL){
    super.onPageFinished(查看,网址);
    如果(url.contains(状态= DONE)){
        //这里开始你的活动
    }
}

如果你不能根据用户正在完成的URL检测,然后事情复杂得多。在这种情况下,你可以尝试添加一个JavaScript,使您可以提取加载页面的HTML,你就会有解析HTML来寻找线索,如果用户完成与否。

I am working in an android application that can post tweets to twitter and I am doing it with the Web View widget. If the user is not logged in it will go to the login screen and if it a logged-in user in it will go to the tweet page. My requirement is after twetting from my application it should return to my application. How can I handle this situation by WebView. How will I get the redirect url from my WebView.

Please help me.

Please look into my code:

public class TestTwittershareActivity extends Activity {
    WebView webview;

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

        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("http://www.twitter.com?status=");
        webview.setWebViewClient(new HelloWebViewClient());
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            return true;
        }
    }
}

解决方案

If I understand your problem correctly, you need a way to check whether the user is done with sending his tweets. Your best bet is to check the url that is being loaded in your webview. Hopefully, this url has some kind of indication that the tweet is done (maybe something in the status part?). To check the url you can use the HelloWebViewClient class you've already created and override it's onPageFinished method. e.g. something like this:

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    if (url.contains("status=DONE")) {
        // start your activity here
    }
}

If you cannot detect based on the url that the user is finished, then things are much more complicated. In that case you can try to add a javascript that allows you to extract the html of the loaded page and you'll have to parse the html to look for clues if the user is done or not.

这篇关于获取的WebView导航事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆