开发者

How work Post method on View?

开发者 https://www.devze.com 2023-03-29 08:36 出处:网络
I have some problem with this code, here you can find a simplified version of the problem. public class MyActivity extends Activity {

I have some problem with this code, here you can find a simplified version of the problem.

public class MyActivity extends Activity {

private WebView wv = null;
private CountDownLatch signal = null;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    wv = (WebView) findViewById(R.id.web);
    wv.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            signal.countDown();
  开发者_如何学运维          super.onPageFinished(view, url);
        }
    }
    wv.post(new Runnable() {

        @Override
        public void run() {
            wv.loadUrl("file:///android_asset/MySample.html");
        }
    });

    try {
        signal.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    ElaborateAll(2);
}

}

In debug the code doesn't reach ElaborateAll(2).

Every help will be apprecited.


What you're doing there is:

  1. Adding a custom WebViewClient with a CountDownLatch
  2. Posting a Runnable to the UI thread
  3. Blocking the UI thread

And that's it, your execution stops there, the Runnable will not be executed and thus you'll never loadUrl(); remove the CountDownLatch and the program will loadUrl() as expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消