[android-developers] Re: Showing Toasts in a service from worker threads with service reference.

2009-06-24 Thread Dianne Hackborn
You need to do this from a message queue / looper thread, such as the main
thread.  Just post a message to the main thread and show your toast from
there.

On Wed, Jun 24, 2009 at 12:48 PM, Richard Schilling 
richard.rootwirel...@gmail.com wrote:


 This is somewhat related to


 http://groups.google.com/group/android-developers/browse_thread/thread/e83f452ae9f307b8/cc1a379efbc27b51?lnk=gstq=toast+from+a+thread#cc1a379efbc27b51


 I have a service running in the background.  I have a background
 thread that gets a reference to the service from the application's
 main activity.  But when the background thread calls a method in the
 service to display a toast, I get the Looper not initialized
 exception.  Why, if I have a valid, bound reference to a Service,
 does this still happen?


 public MyActivity extends Activity{

private static MyService service;

public static MyService getMyService()

private ServiceConnection sConnection = new  ServiceConnection(){
public void onServiceConnected(ComponentName className,
 IBinder
 service) {
service =
 ((MyService.MyServiceBinder)service).getService();
}

public void onServiceDisconnected(ComponentName className) {
service = null;
}

};


public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

Intent bindIntent = new Intent(this, NomadService.class);
bindService(bindIntent, sConnection,
 Context.BIND_AUTO_CREATE);



// start the service.
nomadServiceComponent = startService(
new Intent(this, MyService.class));


   new MyThread().start();
}

 }


 public class MyService{

// normal code for MyService omitted for discussion purposes


public void toast(String t){
Context ctx = getApplicationContext();
try{
Toast.makeText(ctx, t, Toast.LENGTH_LONG);

} catch (Throwable th){
Log.i(Nomad, th.getMessage());
th.printStackTrace();
}
}

 }

 public class MyThread extends Thread{

  public void run(){


  MyService s = MyActivity.getMyService();
  if (s != null)
   s.toast(You're toasted.);
   // causes this exception to occur: Can't create
 handler inside thread that has not called Looper.prepare()
  }

 }



 Richard Schilling
 Root Wireless



 



-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Showing Toasts in a service from worker threads with service reference.

2009-06-24 Thread Richard Schilling

Sounds good.  Thanks for the confirmation.

I'd like to write some more documentation for the Looper class.  Where
do I submit the patch?

Richard


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Showing Toasts in a service from worker threads with service reference.

2009-06-24 Thread Dianne Hackborn
You can go here: http://source.android.com/submit-patches

On Wed, Jun 24, 2009 at 1:21 PM, Richard Schilling 
richard.rootwirel...@gmail.com wrote:


 Sounds good.  Thanks for the confirmation.

 I'd like to write some more documentation for the Looper class.  Where
 do I submit the patch?

 Richard


 



-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---