如何每五秒钟获取一次webview的URL?

时间:2016-03-15 14:02:47

标签: android webview

每当我尝试获取webview的url并尝试在日志中显示它时我的应用程序崩溃了。 这是我的代码...... 一切正常,但在完成页面后每次都会崩溃。

public class MainActivity extends AppCompatActivity {

String user;
String password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = getIntent();
   user = i.getStringExtra("username");
   password = i.getStringExtra("password");



    final WebView webView = (WebView) findViewById(R.id.webView);
    final WebSettings webSettings = webView.getSettings();
    webSettings.setBuiltInZoomControls(true);
    webSettings.setJavaScriptEnabled(true);

    webView.loadUrl("http://www.google.com");

    webView.setWebViewClient(new WebViewClient() {


        @Override
        public void onPageFinished(final WebView view, String url) {

            view.loadUrl("javascript: (function() {document.getElementById('ft_un').value= '" + user + "';}) ();");
            view.loadUrl("javascript: (function() {document.getElementById('ft_pd').value= '" + password + "';}) ();");
            view.loadUrl("javascript:(function(){ document.querySelectorAll(\"input[type='submit']\")[0].click();})();");


            super.onPageFinished(view, url);
        }
    });

    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {

            String currentUrl = webView.getUrl();
            Log.i("url", currentUrl);


        }
    }, 5000);




}

}

这就是我得到的错误

FATAL EXCEPTION: Timer-0
                                                                     Process: com.anshuman.cgcautologin, PID: 8609
                                                                     java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'Timer-0'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {256ea4a6} called on null, FYI main Looper is Looper (main, tid 1) {256ea4a6})
                                                                         at android.webkit.WebView.checkThread(WebView.java:2204)
                                                                         at android.webkit.WebView.getUrl(WebView.java:1293)
                                                                         at com.anshuman.cgcautologin.MainActivity$2.run(MainActivity.java:62)
                                                                         at java.util.Timer$TimerImpl.run(Timer.java:284)
                                                                      Caused by: java.lang.Throwable: A WebView method was called on thread 'Timer-0'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {256ea4a6} called on null, FYI main Looper is Looper (main, tid 1) {256ea4a6})
                                                                         at android.webkit.WebView.checkThread(WebView.java:2194)
                                                                         at android.webkit.WebView.getUrl(WebView.java:1293) 
                                                                         at com.anshuman.cgcautologin.MainActivity$2.run(MainActivity.java:62) 
                                                                         at java.util.Timer$TimerImpl.run(Timer.java:284) 

1 个答案:

答案 0 :(得分:1)

WebView.getUrl()需要在UI线程上运行。

    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    String currentUrl = webView.getUrl();
                    Log.i("url", currentUrl);
                }
            });
        }
    }, 5000);

甚至更好:

webView.postDelayed(new Runnable() {
    public void run() {
        String currentUrl = webView.getUrl();
        Log.i("url", currentUrl);
    }
}, 5000);

好的,这不会重演。但实际上,你需要每五秒登录一次吗?在onPageStarted()中覆盖WebViewClient,您已经拥有onPageFinished(),只需记录页面加载的开始时间和结束时间。