Toast notification from within a Service Thread

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

Toast notification from within a Service Thread

Postby cvandeplas » Sat Nov 14, 2009 6:11 pm

Hello,


I read the following tutorials about using and creating Services.
http://minicompact.com/mobiletalk/?p=18
http://minicompact.com/mobiletalk/?p=32
http://developerlife.com/tutorials/?p=356
The Android API is of course also an open reference.

Now I can perfectly start up a Toast from inside my service.
But I cannot find a way to start the Toast from withing a thread of that service.

Small example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public static TunnelDroid PARENT_ACTIVITY;
  3.  
  4. @Override
  5.  
  6.         public void onCreate() {
  7.  
  8.                 super.onCreate();
  9.  
  10.  
  11.  
  12.                 Log.i(getClass().getSimpleName(), "Timer started begin!!!");
  13.  
  14.                 // I should only do CPU intensive stuff in a new thread
  15.  
  16.                 timer.scheduleAtFixedRate(new TimerTask() {
  17.  
  18.                         public void run() {
  19.  
  20.                                 Log.i(getClass().getSimpleName(), "Timer Event!!!");
  21.  
  22.                                 // show a Toast from that new thread DOESNT WORK
  23.  
  24.                                 Toast.makeText(TunnelManagerService.PARENT_ACTIVITY, "Timer Event !!!", Toast.LENGTH_SHORT).show();
  25.  
  26.                         }
  27.  
  28.                 }, 0, UPDATE_INTERVAL);
  29.  
  30.  
  31.  
  32.                 if (PARENT_ACTIVITY != null)
  33.  
  34.                         // Toast works fine
  35.  
  36.                         Toast.makeText(PARENT_ACTIVITY, "TunnelManagerService started", Toast.LENGTH_SHORT).show();
  37.  
  38.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Could someone help me out?

My application needs a background service that can interface with the user using Toasts and other dialogs (user/pass)
But the service itself needs to run in a separate thread as it has a networksocket open the whole time.


Thanks for the help
cvandeplas
Freshman
Freshman
 
Posts: 3
Joined: Sat Nov 14, 2009 6:01 pm

Top

Postby qlimax » Sat Nov 14, 2009 7:06 pm

http://www.helloandroid.com/node/243

use a handler to do ui stuff (toast) from the thread

Hope it helps :)


bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby cvandeplas » Sat Nov 14, 2009 11:29 pm

qlimax wrote:http://www.helloandroid.com/node/243
use a handler to do ui stuff (toast) from the thread


Thanks for the idea, but I already use handlers to pass on events and messages...

I have currently two annoying things there:
- my dialog is more than a yes/no dialog. I need to get the input of the TextEdits and use it in my thread, so I implement some methods to pass on variables...
- my thread needs to wait until it has the input from the user. This is not the case when using a handler as the event is simply dispatched and then the code continues.

I'll try to search for a way to work around these issues.

Thanks for the reply
cvandeplas
Freshman
Freshman
 
Posts: 3
Joined: Sat Nov 14, 2009 6:01 pm

Postby cvandeplas » Sun Nov 15, 2009 2:59 pm

It looks like there are indeed no other ways than doing it like this:

To display the dialog: from the service use a handle to ask an Activity to display the dialog

To send the input to the Service: use an AIDL interface with a function to send your values and objects to the service
cvandeplas
Freshman
Freshman
 
Posts: 3
Joined: Sat Nov 14, 2009 6:01 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests