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 ();p rivate Runnable Runnable = new Runnable () {@Overridepublic void run () {try {T Hread.currentthread (); Thread.Sleep (10000); StartActivity (New Intent (Mainactivity.this, Nextactivity.class));} catch (Interruptedexception e) {e.printstacktrace ();}}};@Overrideprotected 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 () { @Overridepublic void OnClick (View v) {MainActivity.this.startActivity (new Intent (Mainactivity.this, Adsactivity.class));});} Private class LoadImage extends Asynctask<string, String, bitmap> {@Overrideprotected void OnPreExecute () { Super.onpreexecute ();pD ialog = new ProgressDialog (mainactivity.this);pD ialog.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);pD Ialog.dismiss ();handler.postdelayed (runnable, 4000);} else {Pdialog.dismiss (); Toast.maketext (Mainactivity.this, "Image Does not exist or Network Error", Toast.length_short). Show ();}}}
Asynchronous threads with the effect of Thread.Sleep () in OnCreate () are not expected to stay for a few seconds, you can use handler to send messages to achieve the effect ....
Asynctask cannot use solution with Thread.Sleep ()