Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Toast started from Service only displays once

Tags:

android

I have a service that monitors a socket connection. When the connection is lost it needs to display a Toast informing the user that it is reconnecting. This works fine the first time. After that I see the enqueueToast in the log but the toast is not displayed. Any ideas are appreciated. I thought this was going to be an easy thing to add, but I must be missing something.

Log entry

INFO/NotificationService(118): enqueueToast pkg=com.abc callback=android.app.ITransientNotification$Stub$Proxy@43f7b100 duration=1

Code that calls the Toast

public class ConnectionService extends Service 
{ .....

public void restartConnection()
{
  try
  {
     Log.i(this.toString(), "Attempting to reconnect...");

     // increase the wait between each retry until the max is reached
     int sleepTime = reconnectCounter * MIN_RECON_WAIT;

     if (sleepTime > MAX_RECON_WAIT)
     {
        sleepTime = MAX_RECON_WAIT;
     }

     String msg = "The connection has been lost.  Restart attempt will start in: " + sleepTime/1000 + " seconds";

     Log.i(this.toString(), msg);
     Toast.makeText(getApplicationContext(), msg , Toast.LENGTH_LONG).show();

     Thread.sleep(sleepTime);

     // increment the counter
     reconnectCounter++;

     this.startConnectionThread();

  }
  catch (Exception e)
  {
      Log.e(this.toString(), "Exception: " + e.toString());
      e.printStackTrace();
  }
}// end retartConnection
like image 533
bursk Avatar asked Oct 26 '10 15:10

bursk


People also ask

Can we show toast in service?

The implementation of onPause() and onResume() in the app makes sure that the service knows when the app can accept calls. To do a toast popup, add another method to the ICapture interface and implement it in the app. Your service can then call it any time it knows the screen can accept it.

Why is toast not working in android Studio?

Make sure not to forget to call show() after the makeText. Check for the Context , if its the right one. The most important one , make sure your Android Notifications are on for your app, else the Toast will not be shown.

How do you show toast on android?

Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.

How many types of toast are there in android?

There are only 2 constants of Toast class which are given below.


2 Answers

here is the solution

http://www.jjoe64.com/2011/09/show-toast-notification-from-service.html

you have to create a Handler in the onStartCommand method. And in the onHandleIntent method you can then create and show a toast notification

like image 22
appsthatmatter Avatar answered Oct 04 '22 20:10

appsthatmatter


Yeah, you could go with the runOnUiThread, that's a legit way.
Also, you could try the Handler alternative. Either way it should work.

Here is some code from the top of my head. I don't have the SDK now to test it but I think it should give you a general idea.

public class ConnectionService extends Service {  
  private Handler handler = new Handler();

  public void restartConnection(){
     int sleepTime = reconnectCounter * MIN_RECON_WAIT;
     if (sleepTime > MAX_RECON_WAIT)
     {
        sleepTime = MAX_RECON_WAIT;
     }
     String msg = "The connection has been lost.  Restart attempt will start in: " + sleepTime/1000 + " seconds";
     (new Timer()).schedule(
     new TimerTask() {
        public void run() {
           handler.post(new Runnable() {
              public void run() {
                 Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show();
                 reconnectCounter++;
                 this.startConnectionThread()
              }
           });
        }
     }, sleepTime);
  }//end restartConnection

}//end ConnectionService
like image 55
Vuk Avatar answered Oct 04 '22 20:10

Vuk