AsyncTask|AsyncTask 不能与Thread.sleep()同时使用解决方案
public class MainActivity extends Activity { private ImageView iv_ads;
String urrstrString = "http://a.hiphotos.baidu.com/image/pic/item/7af40ad162d9f2d3f7ec95eeabec8a136327cc36.jpg";
Bitmap bitmap;
ProgressDialog pDialog;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.currentThread();
Thread.sleep(10000);
startActivity(new Intent(MainActivity.this, NextActivity.class));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
iv_ads = (ImageView) findViewById(R.id.iv_ads);
new LoadImage().execute(urrstrString);
iv_ads.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
MainActivity.this.startActivity(new Intent(MainActivity.this,
AdsActivity.class));
}
});
} private class LoadImage extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(
urrstrString).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}protected void onPostExecute(Bitmap image) {
if (image != null) {
iv_ads.setImageBitmap(image);
pDialog.dismiss();
handler.postDelayed(runnable, 4000);
} else {
pDialog.dismiss();
Toast.makeText(MainActivity.this,
"Image Does Not exist or Network Error",
Toast.LENGTH_SHORT).show();
}} }
}
【AsyncTask|AsyncTask 不能与Thread.sleep()同时使用解决方案】异步线程与直接在oncreate() 中Thread.sleep()达不到预期的停留几秒的效果,可以用Handler发送消息,来实现效果...............
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 2018-02-06第三天|2018-02-06第三天 不能再了,反思到位就差改变
- Docker应用:容器间通信与Mariadb数据库主从复制
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 第326天
- Shell-Bash变量与运算符
- 逻辑回归的理解与python示例
- Guava|Guava RateLimiter与限流算法
- 我和你之前距离
- 良心