Android在非UI线程中显示Toast
2016-04-27 23:01
423 查看
public void showToast(String msg){
Looper.prepare();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
Looper.loop();
}
只需要加上那两句就能在非UI线程中显示Toast
[java] view
plain copy
Toast里面的show()
public void show() {
...
service.enqueueToast(pkg, tn, mDuration); //把这个toast插入到一个队列里面
...
}
[java] view
plain copy
Looper
public static final void prepare() {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper()); //在当前线程中创建一个Looper
}
private Looper() {
mQueue = new MessageQueue(); //关键在这,创建Looper都干了什么。 其实是创建了消息队列
mRun = true;
mThread = Thread.currentThread();
}
总结下:Toast 显示的必要条件:
1:Toast 显示需要出现在一个线程的消息队列中.... 很隐蔽
Looper.prepare();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
Looper.loop();
}
只需要加上那两句就能在非UI线程中显示Toast
[java] view
plain copy
Toast里面的show()
public void show() {
...
service.enqueueToast(pkg, tn, mDuration); //把这个toast插入到一个队列里面
...
}
[java] view
plain copy
Looper
public static final void prepare() {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper()); //在当前线程中创建一个Looper
}
private Looper() {
mQueue = new MessageQueue(); //关键在这,创建Looper都干了什么。 其实是创建了消息队列
mRun = true;
mThread = Thread.currentThread();
}
总结下:Toast 显示的必要条件:
1:Toast 显示需要出现在一个线程的消息队列中.... 很隐蔽
相关文章推荐
- ajax异步处理时,如何在JS中获取从Servlet或者Action中session,request
- Android系统Surface机制的SurfaceFlinger服务渲染应用程序UI的过程分析
- Hue简介
- hdu 5667 Sequence(BC——矩阵快速幂)
- 如何在UITableView里动态添加 cell
- 几种常用容器的分析与比较
- IOS开发-UIView之动画效果的实现方法(
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(七)
- 开发测试时给 Kafka 发消息的 UI 发送器――Mikasa
- 详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
- leetcode-303. Range Sum Query - Immutable
- Android 图文混排spannableStringBuilder简单使用
- IDEA 主题、字体、编辑区主题、文件编码修改
- 设置UITabBarController的用法
- js/jquer获取web页面上传文件的大小
- sdut 2605 Mountain Subsequences(树状数组)
- Implement Stack using Queues
- xcode7.3出现the behavior of the UICollectionViewFlowLayout is not defined警告
- Codeforces--237E--Build String(最小费用流)
- note:debugging requires the debug connect session system privilege