Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - calling ui thread from worker thread

Tags:

Hi I want to make Toast available to me no-matter-what and available from any thread whenever I like within my application. So to do this I extended the Activity class:

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));     } } 

so that all Activity classes in my app are now simply

public class AppMain extends MyActivity {    //blah } 

what I expected to be able to do (in a worker thread) was this:

try{    MyActivity me = (MyActivity) Looper.getMainLooper().getThread();    me.doToast("Hello World"); } catch (Exception ex){    Log.e("oh dear", ex.getMessage()); } 

and so long as the Activity was a "MyActivity" it should work - but the problem is ---> the Looper.getMainLooper().getThread(); isn't returning the MyActivity to me and it's making me cry - what am I doing wrong?

: EDIT :

some background to explain "why" I am stuck with this type of implmentation.

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 all well and good.. but if there is NO Internet most (99.999% of Android apps lame /pathetic / mewling at this, and basically offer the user no plan "b" assuming that at all times the internet is there - when it is NOT)

My App will not "go lame (as I call it)" - it does have a plan "b" 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 Toast that as a message to the user (EG: "your form was sent")

like image 552
conners Avatar asked Dec 06 '12 15:12

conners


People also ask

What is UI thread and worker thread?

People use the word "worker" when they mean a thread that does not own or interact with UI. Threads that do handle UI are called "UI" threads. Usually, your main (primary) thread will be the thread that owns and manages UI. And then you start one or more worker threads that do specific tasks.

Why is the main thread sometimes also called UI thread?

This thread is in charge of the user interface. Every additional component is also run on this thread, unless explicitly instructed otherwise. It is also the only thread that may update the user interface. For this reason, it's also often referred to as a UI thread.

Does handler run on UI thread?

A Handler allows communicating back with UI thread from other background thread . This is useful in android as android doesn't allow other threads to communicate directly with UI thread. A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue.


1 Answers

What's wrong with runOnUiThread?

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

activity.runOnUiThread(new Runnable() {     public void run() {         Toast.makeText(activity, "Hello, world!", Toast.LENGTH_SHORT).show();     } }); 
like image 132
Rawkode Avatar answered Jan 19 '23 13:01

Rawkode