无法使用线程或计时器每10秒重新加载一次webView

时间:2016-03-15 16:09:38

标签: android multithreading webview timer

我尝试了大多数方法每10秒重新加载一次webview,例如使用线程,计时器,ScheduledExecutiveService ......但每次尝试时,我的应用都会崩溃。 这是我的代码......

public class MainActivity extends AppCompatActivity {

String user;
String password;
WebView webView;

@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");


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

//在此之后我收到错误这是我的计时器,下面是重新加载webview的代码

    new Timer().schedule(new TimerTask(){
        @Override
        public void run() {
            // TODO Auto-generated method stub
            refresh();
        }}, 10000, 10000);

}
public void refresh() {

    // TODO Auto-generated method stub
    webView.loadUrl("javascript:window.location.reload(true)");


  }
}

我得到的错误就在这里:

FATAL EXCEPTION: Timer-0
Process: com.anshuman.cgcautologin, PID: 5318
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.loadUrl(WebView.java:851)
at com.anshuman.cgcautologin.MainActivity.refresh(MainActivity.java:72)
at com.anshuman.cgcautologin.MainActivity$2.run(MainActivity.java:64)
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.loadUrl(WebView.java:851) 
at com.anshuman.cgcautologin.MainActivity.refresh(MainActivity.java:72) 
at com.anshuman.cgcautologin.MainActivity$2.run(MainActivity.java:64) 
at java.util.Timer$TimerImpl.run(Timer.java:284)

3 个答案:

答案 0 :(得分:1)

您的错误消息告诉您:All WebView methods must be called on the same thread.

您可以使用事件循环模式解决此问题:让您的主线程运行一个事件循环,然后发布一个事件,以便您的主线程将其拾取并在该线程中运行它。

答案 1 :(得分:1)

错误说:

  

必须在同一个线程上调用所有WebView方法。

因此,更改要在主线程中完成的刷新代码。或者是具有webview初始化的那个。像这样:

//In the activity
Handler handler = new Handler(this);

// Your refresh function
 public void refresh() {

      handler.post(new Runnable() {
              @Override
             public void run() {
            webView.loadUrl("javascript:window.location.reload(true)");

      }
 });}

答案 2 :(得分:0)

Android允许您仅在主线程上更新UI组件,您可以使用处理程序:

final int MSG_UPDATE = 0;
Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        switch(msg.what) {
            case MSG_UPDATE:
                //update ur ui component here
                refresh();
        }
        return false;
    }
});
public void refreshWithDelay(long delayMillis) {
    handler.sendEmptyMessageDelayed(MSG_UPDATE, delayMillis);
}
public void refresh() {
    //Your implementation
}

上面的代码在主线程上执行此操作,您只需要在需要刷新视图的位置调用refreshWithDelay