Android, How to get redirection event in WebView?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_loginweb);

    Log.i(TAG, "Try to create activity...");

    final String userId = PropertyManager.getUserId();
    final String password = PropertyManager.getPassword();
    Log.i(TAG, "Id: " + userId + ", Password: " + password);

    final WebView webView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "JsInterface");
    webView.setWebViewClient(new WebViewClient() {  
        @Override  
        public void onPageFinished(WebView view, String url) {  
            webView.loadUrl("javascript:document.LoginForm.tempusername.value = '" + userId + "';");
            webView.loadUrl("javascript:document.LoginForm.password.value = '" + password + "';"); 
            webView.loadUrl("javascript:document.LoginForm.Submit.click();");

            webView.loadUrl("javascript:window.JsInterface.processHTML(document.getElementsByTagName('html')[0].innerHTML);");

            String currentUrl = webView.getUrl();
            Log.i(TAG, "current Url is: " + currentUrl);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            super.shouldOverrideUrlLoading(view, url);

            Log.i(TAG, "redirect to: " + url);
            view.loadUrl(url);
            return false;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);

            Log.i(TAG, "error code is:" + errorCode);
        }
    });  

    webView.loadUrl(PropertyManager.getLoginPageURL());
}


/*
 * To bind a new interface between our JavaScript and Android code, call addJavascriptInterface(), 
 * passing it a class instance to bind to JavaScript of login page and an interface name that 
 * JavaScript can call to access the class.
 */
public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** process the html as needed by the app */
    public void processHTML(String html) {
        Log.i(TAG, "Processing HTML...");
        Log.i(TAG, "content of page: " + html);
    }

    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值