Arun PS
Arun PS

Reputation: 4650

Get the WebView navigation event

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;
        }
    }
}

Upvotes: 0

Views: 4132

Answers (1)

THelper
THelper

Reputation: 15619

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.

Upvotes: 3

Related Questions