Asynctask cannot use solution with Thread.Sleep ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.