Hi, Most of time, ANR happened when main thread blocked. So I use another thread to handle 'time-consuming' operation, after receiving thread callback then invoke main thread.
On Jun 11, 5:23 pm, NightGospel <[email protected]> wrote: > Hi all, > > Be one of Android developers, you must be aware of the > ANR(Application Not Responding) very well and understand how to avoid > it. There are many ways you can choose to do. I usually create a > thread and use a flag > to trigger it to handle time-consuming tasks. For example: > > public class ActivityA extends Activity implements OnClickListener{ > private boolean isDestroyed = false; > private FetchThread thread; > > public void onCreate(Bundle b){ > super.onCreate(b); > . > . > } > > public void onDestroy(){ > super.onDestroy(); > isDestroyed = true; > } > > public void onClick(View view){ > thread = new FetchThread(); > thread.setStartFlag(true); > thread.start(); > } > > class FetchThread extends Thread{ > private boolean shouldStart = false; > public void run(){ > while (!isDestroyed){ > if (shouldStart){ > shouldStart = false;; > // do time-consuming task > // after task is finished, use handler to update > UI > } > try{ > sleep(500); > } catch (Exception ex){} > > } > } > > public void setStartFlag(boolean b) { shouldStart = b;} > } > > } > > The above is the way I usually do and it's as the same as running one > background service. Does anyone be able to provide your solution? I > think my method is not very smart. :( > > NightGospel -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

