Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

Android: Toast is not showing before system.exit()

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

EDIT** Why all the down votes? 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 (although I very much appropriate the responses) do not work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thank you all who tried answering this for me, but unfortunately the solutions did not work.

Turns out that I have to do this when I try using Toast within a Service.

Once again, to those who tried answering it: thank you.

To those who down voted my post without stating the reason, next time leave constructive criticism rather than leaving me in the dark. I'm here to learn and I can't learn if you simply down vote with providing a legit reason. I'm not a mind reader.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...