title: how to call asynctask from call which extends Service
body: <p>How to call Asynctask from runnable. In my class i am extending Service so i dont know what is the problem. The Log cat output is like</p>
  
  <pre><code>    05-02 11:44:40.914: E/AndroidRuntime(1997): FATAL EXCEPTION: main
      05-02 11:44:40.914: E/AndroidRuntime(1997):       
      android.view.WindowManager$BadTokenException: Unable to add window -- token null is 
      not     for an application
      05-02 11:44:40.914: E/AndroidRuntime(1997):     at  
      android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
      05-02 11:44:40.914: E/AndroidRuntime(1997):     at  
      android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
      05-02 11:44:40.914: E/AndroidRuntime(1997):     at 
      android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
      05-02 11:44:40.914: E/AndroidRuntime(1997):     at 
      android.app.Dialog.show(Dialog.java:281)
      05-02 11:44:40.914: E/AndroidRuntime(1997):     at  
    com.example.sebi.BroadcastService$SaveProducts.onPreExecute(BroadcastService.java:55)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at  
     android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at  
     android.os.AsyncTask.execute(AsyncTask.java:534)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
     com.example.sebi.BroadcastService$1.run(BroadcastService.java:39)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
     android.os.Handler.handleCallback(Handler.java:725)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
     android.os.Handler.dispatchMessage(Handler.java:92)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at  
     android.os.Looper.loop(Looper.java:137)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
     android.app.ActivityThread.main(ActivityThread.java:5039)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at                   
     java.lang.reflect.Method.invokeNative(Native Method)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
     java.lang.reflect.Method.invoke(Method.java:511)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
     05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
     dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>and when I tried using <strong>runOnUiThread</strong> I got error at <strong>runOnUiThread</strong>.</p>
  
  <pre><code>      The method runOnUiThread(new Runnable(){}) is undefined for the type new  
        Runnable(){}
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>       private Runnable sendUpdatesToUI = new Runnable() {
          public void run() {
              runOnUiThread(new Runnable(){
  
              public void run(){
              new SaveProducts().execute();  
              handler.postDelayed(this, 30000); // 10 seconds
          }
              });
          }
      };    
  
      class SaveProducts extends AsyncTask&lt;String, String, String&gt; {
  
          @Override
          protected void onPreExecute() {
  
              super.onPreExecute();
  
                  pDialog = new  
             ProgressDialog(BroadcastService.this);
                  pDialog.setMessage("loading.");
                  pDialog.setIndeterminate(false);
                  pDialog.setCancelable(true);
                  pDialog.show();
  
          }
  </code></pre>