multithreading - Android: How to work with background thread? -


i've developed application takes content internet , shows accordingly on device's screen . program works fine , little bit slow . takes 3-4 seconds load , display content . put code fetches content , displays in background thread , while program doing functions , display progress dialog. me ? learn how put code in background thread.

my code

public class activity1 extends activity {     private progressdialog progressdialog;      @override     public void oncreate(bundle savedinstancestate)     {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);          new asynctask<integer, integer, boolean>()         {             progressdialog progressdialog;              @override             protected void onpreexecute()             {                 /*                  * executed on ui thread before doinbackground().                  * perfect place show progress dialog.                  */                 progressdialog = progressdialog.show(activity1.this, "",                         "loading...");             }              @override             protected boolean doinbackground(integer... params)             {                 if (params == null)                 {                     return false;                 }                 try                 {                     /*                      * run on background thread, can sleep here                      * or whatever want without blocking ui thread. more                      * advanced use download chunks of fixed size , call                      * publishprogress();                      */                     thread.sleep(params[0]);                     // here i've put functions work me                 }                 catch (exception e)                 {                     log.e("tag", e.getmessage());                     /*                      * task failed                      */                     return false;                 }                  /*                  * task succeeded                  */                 return true;             }              @override             protected void onpostexecute(boolean result)             {                 progressdialog.dismiss();                 /*                  * update here view objects content download.                  * save dismiss dialogs, update views, etc., since                  * working on ui thread.                  */                 alertdialog.builder b = new alertdialog.builder(activity1.this);                 b.settitle(android.r.string.dialog_alert_title);                 if (result)                 {                     b.setmessage("download succeeded");                 }                 else                 {                     b.setmessage("download failed");                 }                 b.setpositivebutton(getstring(android.r.string.ok),                         new dialoginterface.onclicklistener()                         {                              @override                             public void onclick(dialoginterface dlg, int arg1)                             {                                 dlg.dismiss();                             }                         });                 b.create().show();             }         }.execute(2000);          new thread()         {             @override             public void run()             {                  // dismiss progressdialog                 progressdialog.dismiss();             }         }.start();     } } 

check asynctask, created such tasks.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -