简体   繁体   中英

Android listener for webpage to start loading?

I have a simple webview that loads a website. 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.

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. :(

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.

Does anyone have any suggestions? I would greatly appreciate it! :)

EDIT:

Here's the entirety (minus the import headers) of my MainActivity.java class.

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. After a user logs in, the page (obviously) starts to load and directs to another post-login URL. I'd like to check the URL with an if statement after the page is done loading for the second time.

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. 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. I had a similar issue here:

Closing a Webview in Android

So basically you just want to execute some JavaScript once you've logged in? 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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