Toast from thread

Put your problem here if it does not fit any of the other categories.

Toast from thread

Postby Themuzz » Sat Nov 07, 2009 11:08 pm

Hello,

I have an activity which calls a SurfaceView which starts an thread. The problem is that inside the thread I check some things and want to show a toast on the window.

This does not work if I use the context of the activity and I found that a toast cannot be started from a thread. So I tried using the Looper solution:

Looper.prepare();
Toast.makeText(context_main, "test", 300).show();
Looper.loop();
Looper.myLooper().quit();

This does show the message but the thread and the activity both hang afterwards. So I'm wondering if anyone could share some code snippet with how I should do it :)

Thanx for the help!
Themuzz
Freshman
Freshman
 
Posts: 9
Joined: Tue Oct 20, 2009 8:20 pm

Top

Postby sudheer1437 » Sun Nov 08, 2009 11:07 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Thread action = new Thread()
  2.  
  3. {
  4.  
  5.   public void run()
  6.  
  7.   {
  8.  
  9.       try
  10.  
  11.       {
  12.  
  13.          Toast.makeText(main.this, "Hiii.", Toast.LENGTH_SHORT).show();
  14.  
  15.       }
  16.  
  17.       catch (Exception e) {  }
  18.  
  19.   }
  20.  
  21. };
  22.  
  23. this.runOnUiThread(action);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


try this code...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby Emmanuel7 » Sun Nov 08, 2009 1:14 pm

I second that...

I'm just wondering about the try / catch...

Have you been hit by an exception send by a toast ?
I've never seen that...
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby Themuzz » Sun Nov 08, 2009 1:58 pm

Thanx for the help! But I can't get it to work.

My application looks as follow:

startGame.java which is an activity and starts the surfaceview and the thread
gameView.java which extends the surfaceview
gameThread.java which extends the thread. This one wants to post a message to the activity.

But I don't see how that's happening with your code since your code assumes I'm working from one file.

Kind regards,

Themuzz
Themuzz
Freshman
Freshman
 
Posts: 9
Joined: Tue Oct 20, 2009 8:20 pm

Postby sudheer1437 » Sun Nov 08, 2009 2:21 pm

Hi Emmanuel7

that try.. catch...

Actually i copied and edited it from my code...
so i forgot to remove that try..catch...

sorry...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby Themuzz » Sun Nov 08, 2009 2:24 pm

Hehe, Did you read my reply? :)
Themuzz
Freshman
Freshman
 
Posts: 9
Joined: Tue Oct 20, 2009 8:20 pm

Top

Postby sudheer1437 » Sun Nov 08, 2009 2:27 pm

yup..
i have read ur reply...
now i m trying tat only..
i will get back to u.. if i get a solution...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby Themuzz » Sun Nov 08, 2009 2:55 pm

I found something that works:

Inside the startGame were I start the thread I also add a handler:

mgameThread = new gameThread(d.getHeight(), d.getWidth(), new Handler(){
@Override
public void handleMessage(Message m) {
Toast.makeText(getBaseContext(), m.getData().getString("text"), m.getData().getInt("time")).show();
}
});

Inside the thread I use that handler to post a message:

Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putString("text", "Hallllllo");
b.putInt("time", 300);
msg.setData(b);

mHandler.sendMessage(msg);

I hope this is the correct way of doing so? Let me know :)
Themuzz
Freshman
Freshman
 
Posts: 9
Joined: Tue Oct 20, 2009 8:20 pm

Postby ExxKA » Sun Nov 08, 2009 5:12 pm

Themuzz wrote:I found something that works:

Inside the startGame were I start the thread I also add a handler:
......
I hope this is the correct way of doing so? Let me know :)


Hey. I was just about to tell you to use a handler. Yes it's the right way to do postbacks between different threads on Android.
ExxKA
Developer
Developer
 
Posts: 29
Joined: Tue Nov 03, 2009 7:16 pm
Location: Denmark

Postby Themuzz » Sun Nov 08, 2009 6:00 pm

Thanx :)

But now I have the problem that I want to show a toast for a specified time (in miliseconds), which is not possible for a toast (only long or short).

I want to accomplish this with an alertdialog but I don't know how I can use the code I posted to show an alert and dismiss it after a specified amount of time.

Could someone help and post a code snippet for the example I posted in my previous post?

Kind regards,

Themuzz
Themuzz
Freshman
Freshman
 
Posts: 9
Joined: Tue Oct 20, 2009 8:20 pm

Re: Toast from thread

Postby vegolath » Thu Jan 06, 2011 10:35 am

hi, as far a you know, an android application is running with one thread which is the only one that support GUI changes / update (it called the Main / GUI thread), every component that you register in the application manifest.xml (Activities, Services and BroadcastReceivers) runs in this Main / GUI thread.

To solve your problem you can create an Hander class in one of your components (Not recommended in the Broadcast Receiver) to support message calling and fire a toast as well:

Code: Select all
Handler someHandler = new Handler(){
     
     //this method will handle the calls from other threads.      
     public void handleMessage(Message msg) {
            
          Toast.makeText(getBaseContext(), msg.getData().getString("SOMETHING"), Toast.LENGTH_SHORT).show();
     }
};



Now, all u have 2 do is to call it from your thread and loop it:

Code: Select all
        Looper.prepare();
         
        //create the message for the handler
   Message status = someHandler.obtainMessage();
   Bundle data = new Bundle();
   data.putString("SOMETHING", msgContent);
   status.setData(data);
         
       someHandler.sendMessage(status);
         
      Looper.loop();


The Main / GUI thread has a queue for GUI updates, we run it in other threads by using the Looper mechanism.

Best regards,

Vegolath.
vegolath
Freshman
Freshman
 
Posts: 2
Joined: Sun Mar 29, 2009 2:53 pm

Re: Toast from thread

Postby nymble » Wed Mar 30, 2011 2:26 pm

Vegolath, that solution worked great for me. Thanks much!
nymble
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 30, 2011 2:24 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 19 guests