Android:在 system.exit() 之前没有显示 Toast [英] Android: Toast is not showing before system.exit()

查看:28
本文介绍了Android:在 system.exit() 之前没有显示 Toast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我嵌套了嵌套类,在退出应用程序之前,我试图在其中显示最外层类的 Toast.如果我注释掉 exit 语句,toast 工作得很好,所以我知道我正在正确访问上下文.我还尝试将 Toast 放在一个线程中,它会休眠 2000 毫秒(对于退出语句,反之亦然),但这仍然不起作用.

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** 为什么全是反对票?我没有使用任何活动(最外面的类是一个 Service,而两个内部是普通的 Java 类),所以一些答案(尽管我非常适合回答)不起作用.

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.

推荐答案

感谢所有尝试为我回答这个问题的人,但不幸的是这些解决方案没有奏效.

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

事实证明我必须做这个 当我尝试在 Service 中使用 Toast 时.

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.

这篇关于Android:在 system.exit() 之前没有显示 Toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆