有一个链接是5秒重定向到另一个页面,如何在重定向之前做出延迟 以下是一个示例链接:http://s.spynetstation.com/m/Logistics/Logistics%20-%20Medical%20History%20NHSDL09,%202007/05%20Logistics%20-%20Safe%20On%20My%20Side.mp3 的Сode
public void ShowTrack(String smetaout,String stitleout){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(stitleout);
WebView wv = new WebView(this);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
Log.d("My Webview", "shouldOverrideUrlLoading"+urlNewString);
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(urlNewString);
Log.d("My Webview", urlNewString);
return false;
}
public void onPageStarted(WebView view, String url) {
Log.d("My Webview", "onPageStarted"+url);
loadingFinished = false;
}
@Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
} else{
redirect = false;
}
Log.d("My Webview", "onPageFinished"+url);
}
});
wv.loadUrl(smetaout);
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert.show();
}
答案 0 :(得分:1)
试试这个:
public void ShowTrack(String smetaout,String stitleout){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(stitleout);
WebView wv = new WebView(this);
// Progress dialog starts
ProgressDialog progressDialog = ProgressDialog.show(YourActivity.this, "", "Loading... ",true ,true);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
Log.d("My Webview", "shouldOverrideUrlLoading"+urlNewString);
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(urlNewString);
Log.d("My Webview", urlNewString);
return false;
}
public void onPageStarted(WebView view, String url) {
Log.d("My Webview", "onPageStarted"+url);
loadingFinished = false;
}
@Override
public void onPageFinished(WebView view, String url) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
progressDialog.dismiss();
}
}, 5000);
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
} else{
redirect = false;
}
Log.d("My Webview", "onPageFinished"+url);
}
});
wv.loadUrl(smetaout);
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert.show();
}
我希望这就是你想要的。
答案 1 :(得分:1)
在.loadUrl
之前插入任何延迟(定时器,异步等)。例如:
new CountDownTimer( 5000, 5000)
{
public void onTick( long millisUntilFinished) {}
public void onFinish()
{
view.loadUrl(urlNewString);
}
}.start();