title: android - calling ui thread from worker thread
body: <p>Hi I want to make <code>Toast</code> available to me no-matter-what and available from any thread whenever I like within my application. So to do this I extended the <code>Activity</code> class:</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.widget.Toast;
  
  public class MyActivity extends Activity{
      private Handler mHandler;
  
      @Override   
      public void onCreate(Bundle savedInstanceState) {       
          mHandler = new Handler();
          super.onCreate(savedInstanceState);
      }
  
      private class ToastRunnable implements Runnable {
          String mText;
  
          public ToastRunnable(String text) {
              mText = text;
          }
  
          public void run(){
             Toast.makeText(getApplicationContext(), mText, Toast.LENGTH_SHORT).show();
          }
      }
  
      public void doToast(String msg) {
          mHandler.post(new ToastRunnable(msg));
      }
  }
  </code></pre>
  
  <p>so that <strong>all</strong> <code>Activity</code> classes in my app are now simply</p>
  
  <pre><code>public class AppMain extends MyActivity {
     //blah
  }
  </code></pre>
  
  <p>what I expected to be able to do (in a worker thread) was this:</p>
  
  <pre><code>try{
     MyActivity me = (MyActivity) Looper.getMainLooper().getThread();
     me.doToast("Hello World");
  }
  catch (Exception ex){
     Log.e("oh dear", ex.getMessage());
  }
  </code></pre>
  
  <p>and so long as the <code>Activity</code> was a "<code>MyActivity</code>" it should work - but the problem is ---> the <code>Looper.getMainLooper().getThread();</code> isn't returning the <code>MyActivity</code> to me and it's making me cry - what am I doing wrong? </p>
  
  <p><strong>: EDIT :</strong></p>
  
  <p>some background to explain "why" I am stuck with this type of implmentation.</p>
  
  <p>I need to be able to confirm to the user that a "HTTP POST" event has been a success. Now. If the User clicks "OK" on the UI Form it MAY or MAY NOT have internet at that time.. If it has Internet - all well and good - it posts the form via HTTP POST <strong>all well and good</strong>.. but if there is <strong>NO Internet</strong> most (99.999% of Android apps lame /pathetic / mewling at this, and basically offer the user no <strong>plan "b"</strong> assuming that at all times the internet is there - when it is NOT) </p>
  
  <p>My App will not "go lame (as I call it)" - it does have a <strong>plan "b"</strong> instead it "Queues" the post event and retries every x minutes.. now this is a silent thread in the background.. I have plenty of user interaction all over the app I don't know where the user will "be" but eventually when the HTTP POST that queue/retries/queue/retries returns "! Success! " I want to <code>Toast</code> that as a message to the user  (EG: "your form was sent")</p>