使用面包里面的TimerTask [英] Using toast inside timertask

查看:114
本文介绍了使用面包里面的TimerTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现有人在网上有类似的问题,这里

Found someone has similar issue online here.

这不工作:

Timer t = new Timer(false);
t.schedule(new TimerTask() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show();
}
}, 5000);

但是,如果我实例化敬酒TimerTask的外面再运行,它的作品里面表现出来。

But if I instantiate the toast outside the timertask then show it inside run,it works.

我想这可能涉及到所谓的UI线程,

I think it may relate to the so-called UI thread,

但究竟怎么了?

推荐答案

试试这个

Timer t = new Timer(false);
t.schedule(new TimerTask() {
@Override
public void run() {
       runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show();
            }
        });

    }
}, 5000);

这篇关于使用面包里面的TimerTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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