简体   繁体   English

Android网页侦听器开始加载?

[英]Android listener for webpage to start loading?

I have a simple webview that loads a website. 我有一个加载网站的简单Web视图。 Once the website is loaded, I have the following to focus/scroll to the login box: 网站加载完毕后,我可以将以下内容集中/滚动到登录框:

mWebview.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            mWebview.scrollTo(681, 100);

(I've heard that onPageFinished is deprecated, but it's working for me in 2.2>4.4, so I'm just leaving it for now. (我听说过时onPageFinished已过时,但它在2.2> 4.4中对我有效,因此我暂时将其保留。

Anyway, I would like some sort of way to monitor for when the user actually logs in, that way when the page is done loading for the second time, I can then call some js and forward them to another page, but I have no idea how to do that. 无论如何,我想要一种监视用户实际登录时间的方式,即当页面第二次加载完成时,我可以调用一些js并将其转发到另一个页面,但是我不知道怎么做。 :( :(

I can just do another onPageFinished after they log in, but I don't know how to start monitoring... In other words, I can't start another onPageFinished immediately after the current, because it just redirects automatically. 他们登录后,我只能执行另一个onPageFinished,但是我不知道如何开始监视...换句话说,我不能在当前事件之后立即启动另一个onPageFinished,因为它只是自动重定向。

Does anyone have any suggestions? 有没有人有什么建议? I would greatly appreciate it! 我将不胜感激! :) :)

EDIT: 编辑:

Here's the entirety (minus the import headers) of my MainActivity.java class. 这是MainActivity.java类的整体(减去导入标头)。

public class MainActivity extends Activity {

private WebView mWebview ;

@Override
public void onBackPressed() {
    if(mWebview.canGoBack() == true){
        mWebview.goBack();
    }else{
        super.onBackPressed();
    }
}


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    mWebview  = new WebView(this);

    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
    mWebview.getSettings().setBuiltInZoomControls(true);

    if(android.os.Build.VERSION.SDK_INT >= 11) {
        mWebview.getSettings().setDisplayZoomControls(false);
    }

    final Activity activity = this;

    mWebview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });

    mWebview .loadUrl("http://example.com");
    setContentView(mWebview );


    mWebview.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            mWebview.scrollTo(681, 100);


        }
    }

    );



}

I think I might be missing a curly bracket, but ignore that for now, haha. 我想我可能缺少大括号了,但是暂时不要理会,哈哈。 So anyway, after mWebview.scrollTo (the last line), the webpage is done loading, and it just chills at the login page. 因此,无论如何,在mWebview.scrollTo (最后一行)之后,该网页已完成加载,并且只是在登录页面上mWebview.scrollTo After a user logs in, the page (obviously) starts to load and directs to another post-login URL. 用户登录后,页面(显然)开始加载并定向到另一个登录后URL。 I'd like to check the URL with an if statement after the page is done loading for the second time. 我想在页面第二次加载完成后使用if语句检查URL。

Does that make more sense? 这更有意义吗? Sorry, it's confusing me too, trying to explain it. 抱歉,这也让我感到困惑,试图解释一下。

Usually there will be two separate URLs, one for the post log-in page and one for the pre log-in page. 通常会有两个单独的URL,一个用于登录后页面,一个用于预登录页面。 You can have an if statement that checks the URL to make sure you've logged in, and if you have, then it redirects/closes/whatever. 您可以使用if语句来检查URL以确保您已登录,如果已登录,则它将重定向/关闭/执行任何操作。 I had a similar issue here: 我在这里有一个类似的问题:

Closing a Webview in Android 在Android中关闭Webview

So basically you just want to execute some JavaScript once you've logged in? 因此,基本上,您只想登录后执行一些JavaScript? I don't see why you can't have an if statement to check whether you're logged in or not, and if you're logged in, then execute the js. 我不明白为什么您没有if语句来检查您是否已登录,如果您已登录,请执行js。 Something like this maybe? 像这样的东西?

wv.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        if (url.contains(getString(R.string.loginURL))){
          super.onPageFinished(view, url);
          mWebview.scrollTo(681, 100);
         }else if(url.contains(getString(R.string.loggedInURL))) {
                wv.loadUrl(js goes here);
         }
    }
}

This will execute every time a page is finished loading in your WebView 每当您的WebView的页面加载完成时,就会执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM