AsyncTask 不能与Thread.sleep()同时使用解决方案

简介: public class MainActivity extends Activity { private ImageView iv_ads; String urrstrString = "http://a.
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<String, String, Bitmap> {
		@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();
			}

		}

	}
}

  异步线程与直接在oncreate() 中Thread.sleep()达不到预期的停留几秒的效果,可以用Handler发送消息,来实现效果...............

目录
相关文章
|
4天前
|
Java C# Python
线程等待(Thread Sleep)
线程等待是多线程编程中的一种同步机制,通过暂停当前线程的执行,让出CPU时间给其他线程。常用于需要程序暂停或等待其他线程完成操作的场景。不同语言中实现方式各异,如Java的`Thread.sleep(1000)`、C#的`Thread.Sleep(1000)`和Python的`time.sleep(1)`。使用时需注意避免死锁,并考虑其对程序响应性的影响。
|
6月前
|
Java 编译器 UED
Thread.sleep()总结
Thread.sleep()总结
|
7月前
|
监控 算法 Unix
Thread.sleep(0) 到底有什么用
Thread.sleep(0) 到底有什么用
56 1
|
调度 C++
Thread.sleep(0) vs Thread.sleep(1) vs Thread.yield() vs Object.wait()
Thread.sleep(0) vs Thread.sleep(1) vs Thread.yield() vs Object.wait()
|
Java 调度 C++
你真的了解Thread.sleep(0)吗?以及Thread.sleep(1) vs Thread.sleep(0)
你真的了解Thread.sleep(0)吗?以及Thread.sleep(1) vs Thread.sleep(0)
|
消息中间件 安全 Java
|
监控 算法 Java
闲鱼一面:Thread.sleep(0) 到底有什么用?
闲鱼一面:Thread.sleep(0) 到底有什么用?
闲鱼一面:Thread.sleep(0) 到底有什么用?
|
安全 Java 中间件
Thread.sleep(0)的作用
在源码中经常能看到sleep(0)的操作,今天来总结下sleep(0)的作用到底是啥
446 0
Thread和Runnable实现多线程(一)下
Thread和Runnable实现多线程(一)
124 0
Thread和Runnable实现多线程(一)下
Thread和Runnable实现多线程(一)上
实现多线程,有两种方式,一种是继承 Thread 类,一种是实现 Runnable接口。
196 0
Thread和Runnable实现多线程(一)上