`
tudusi
  • 浏览: 1049731 次
文章分类
社区版块
存档分类
最新评论

WebView loadUrl 时实时显示一个dialog

 
阅读更多

项目需求:WebView在loadUrl的时候显示一个dialog,加载好之后dismiss这个dialog

aidHomeWebView = (WebView)findViewById(R.id.aidHomeWebView);
aidHomeWebView.loadUrl(URL);


aidHomeWebView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
aidHomeWebView.loadUrl(urlNewString);
return true;
}

@Override
public void onLoadResource(WebView view, String url) {
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
ProgressDialogUtil.showProgressDialog(OceanHouseActivity.this);
}

@Override
public void onPageFinished(WebView view, String url) {
//HIDE LOADING IT HAS FINISHED
ProgressDialogUtil.dismissProgressDialog();
}
});


试了上述方法,发现一个问题,有时dialog出现多次,当dialog消失的时候,页面往往还是没有加载好。


So,换别的方法:

You can do it by setting up a Timer which checks for progress of current page by calling getProgress() and if it is less than some threshold after some specified time then you can dismiss the loading of the current page.


代码如下:

Timer timer = new Timer();
TimerTask task = new TimerTask(){
public void run() {
setTitle("hear me?");
while(aidHomeWebView.getProgress() != 100){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ProgressDialogUtil.dismissProgressDialog();
}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.ocean_house);
lay_oceanhouse=(FrameLayout)findViewById(R.id.lay_oceanhouse);
showView();

aidHomeWebView = (WebView)findViewById(R.id.aidHomeWebView);
aidHomeWebView.loadUrl(URL);
ProgressDialogUtil.showProgressDialog(this); // add...
timer.schedule(task, 1000); // add...


returnButton_oceanhouse = (Button)findViewById(R.id.returnButton_oceanhouse);
returnButton_oceanhouse.setOnClickListener(this);
}

成功了!



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics