-1

I have nested nested class where I am trying to display a toast for the outer most class before I exit the app. The toast works just fine if I comment out exit statement, so I know I'm accessing the context correctly. I have also tried putting the toast in a thread where it sleeps for 2000 ms (and vice versa for the exit statement), but that still does not work.

All I want to do display a toast and exit the program. (It would be nice to do it simultaneously, if possible...)

public class A extends Service {
   
    private Context context;
    
    //...

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        context = this;
        //...
        return START_STICKY;
    }

    Handler disToast = new Handler(new Callback() {

         @Override
         public boolean handleMessage(Message msg) {
            Toast.makeText(context, "see ya", Toast.LENGTH_SHORT).show();
            return true;//also tried false, but that did not work...
        }
   });

   private Runnable r = new Runnable() {

       public void run() {


           new CountDownTimer(3000, 1000) {

               public void onFinish() {

                   Message msg=disToast.obtainMessage();
                   msg.obj="my message";
                   disToast.sendMessage(msg);
          
                   handler.removeCallbacks(updateTimerThread);
                   System.exit(0);
               }
           }.start();//end of inner most class
            
    };//end of first inner class

}//outermost class

I'm not working with any Activities (outer most class is a Service, and the two inner are normal Java classes) so some of the answers do not work.

E_net4
  • 27,810
  • 13
  • 101
  • 139
bob dylan
  • 647
  • 1
  • 10
  • 25

3 Answers3

0

USE getApplicationConext() INSTEAD OF CONTEXT

Toast.makeText(getApplicationConext(), "Timer up. Existing app...", Toast.LENGTH_SHORT).show();
Bajirao Shinde
  • 1,356
  • 1
  • 18
  • 26
  • I get a "cannot resolve method" error when I try that...perhaps because the inner most class is not an activity? – bob dylan Feb 23 '16 at 06:44
0
Handler disToast= new Handler(new Callback() {
   @Override 
   public void handleMessage(Message msg) { 
             String mString=(String)msg.obj;
             Toast.makeText(this, mString, Toast.LENGTH_SHORT).show();
          }
});

 private Runnable r = new Runnable() {


    public void run() {


        new CountDownTimer(3000, 1000) {

            public void onFinish() {



            Message msg=disToast.obtainMessage();
            msg.obj="your message";
            disToast.sendMessage(msg);

                handler.removeCallbacks(updateTimerThread);
                System.exit(0);
            }
        }.start();

};

More details Refer Here

Community
  • 1
  • 1
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • Thanks for the response, I have tried that (with some modifications so it would compile) but it does not work. I have updated my code above... – bob dylan Feb 23 '16 at 07:11
  • That does not work either, unfortunately. I should also note that the 2nd parameter for makeText gives me an error if its a Message type, rather than a String. – bob dylan Feb 23 '16 at 07:21
  • Message msg=disToast.obtainMessage(); msg.obj="your message"; disToast.sendMessage(msg); – sasikumar Feb 23 '16 at 07:22
  • I tried that...did not work. I have updated the code again – bob dylan Feb 23 '16 at 07:25
  • Thanks for your help, but that does not work either. Also, handleMessage would return a boolean, assuming it is imported from android.os.Handler.Callback; – bob dylan Feb 23 '16 at 07:41
-1

please use

Toast.LENGTH_LONG instead of

Toast.LENGTH_SHORT


 Toast.makeText(context, "Timer up. Existing app...", Toast.LENGTH_LONG).show();

Now your toast will be display after System.exit();

dipali
  • 10,966
  • 5
  • 25
  • 51