title: Android: How to make async task sleep , while evaluating some condition
body: <p>I have a <code>AsyncTask</code> in which I want to test a condition every 3 seconds. </p>
  
  <p>When I call the <code>Thread.sleep(int)</code> method in it I get <code>Can't create handler inside thread that has not called Looper.prepare()</code> error. </p>
  
  <p>Below is my code:</p>
  
  <pre><code>public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
  
      new AsyncTask&lt;Void, Void, Void&gt;() 
      {
  
          ProgressDialog progressDialog;
  
          @Override
          protected void onPreExecute() 
          {
              progressDialog = ProgressDialog.show(getActivity(), null, "Loading Categories...");
              super.onPreExecute();
          }
          @Override
          protected Void doInBackground(Void... voids) 
          {
              // TODO Auto-generated method stub
              while(mOnlinCouponCategoriesManager.isDownloading())
              {
                  try
                  {
                      Thread.sleep(3000);
                  }
                  catch(Exception e)
                  {
                      Log.e("Categories Section:", e.getMessage());
                  }
              }
              if(categoryAdapter  == null)
              {
                  categoryAdapter = new CategoryAdapter(getActivity(), R.layout.fragment_categorylist, mOnlineCategoriesCursor, new String[] {}, new int[] {});
                  setListAdapter(categoryAdapter);
              }
              else
              {
                  setListAdapter(categoryAdapter);
              }
              return null;
          }
  
      }.execute();
  
      View view = inflater.inflate(R.layout.custom_list, null);
      return view;
  }
  </code></pre>
  
  <p>Here is my stack trace</p>
  
  <pre><code>01-15 16:23:38.233: E/AndroidRuntime(31508): FATAL EXCEPTION: AsyncTask #1
  01-15 16:23:38.233: E/AndroidRuntime(31508): Process: in.coupondunia.androidapp, PID: 31508
  01-15 16:23:38.233: E/AndroidRuntime(31508): java.lang.RuntimeException: An error occured while executing doInBackground()
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at java.lang.Thread.run(Thread.java:841)
  01-15 16:23:38.233: E/AndroidRuntime(31508): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.os.Handler.&lt;init&gt;(Handler.java:200)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.os.Handler.&lt;init&gt;(Handler.java:114)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.support.v4.widget.CursorAdapter$ChangeObserver.&lt;init&gt;(CursorAdapter.java:455)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:176)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.support.v4.widget.CursorAdapter.&lt;init&gt;(CursorAdapter.java:122)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.support.v4.widget.ResourceCursorAdapter.&lt;init&gt;(ResourceCursorAdapter.java:54)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.support.v4.widget.SimpleCursorAdapter.&lt;init&gt;(SimpleCursorAdapter.java:63)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at in.coupondunia.androidapp.adapters.CategoryAdapter.&lt;init&gt;(CategoryAdapter.java:27)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at in.coupondunia.androidapp.fragments.online.OnlineCategoriesFragment$1.doInBackground(OnlineCategoriesFragment.java:80)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at in.coupondunia.androidapp.fragments.online.OnlineCategoriesFragment$1.doInBackground(OnlineCategoriesFragment.java:1)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  01-15 16:23:38.233: E/AndroidRuntime(31508):    ... 4 more
  01-15 16:23:39.434: E/WindowManager(31508): android.view.WindowLeaked: Activity in.coupondunia.androidapp.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42843b18 V.E..... R......D 0,0-729,192} that was originally added here
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewRootImpl.&lt;init&gt;(ViewRootImpl.java:348)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.app.Dialog.show(Dialog.java:286)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.app.ProgressDialog.show(ProgressDialog.java:116)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.app.ProgressDialog.show(ProgressDialog.java:99)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.app.ProgressDialog.show(ProgressDialog.java:94)
  01-15 16:23:39.434: E/WindowManager(31508):     at in.coupondunia.androidapp.fragments.online.OnlineCategoriesFragment$1.onPreExecute(OnlineCategoriesFragment.java:59)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.os.AsyncTask.execute(AsyncTask.java:535)
  01-15 16:23:39.434: E/WindowManager(31508):     at in.coupondunia.androidapp.fragments.online.OnlineCategoriesFragment.onCreateView(OnlineCategoriesFragment.java:90)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.View.measure(View.java:16497)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.View.measure(View.java:16497)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.View.measure(View.java:16497)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.View.measure(View.java:16497)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.View.measure(View.java:16497)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.View.measure(View.java:16497)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  01-15 16:23:39.434: E/WindowManager(31508):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.View.measure(View.java:16497)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  01-15 16:23:39.434: E/WindowManager(31508):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.View.measure(View.java:16497)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.Choreographer.doFrame(Choreographer.java:544)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.os.Handler.handleCallback(Handler.java:733)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.os.Handler.dispatchMessage(Handler.java:95)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.os.Looper.loop(Looper.java:136)
  01-15 16:23:39.434: E/WindowManager(31508):     at android.app.ActivityTh
  </code></pre>
  
  <p>How Can I make async task wait for 3 seconds before it checks the condition?</p>