工具类ToastUtil 避免在子线程中使用抛异常 "Can't create handler inside thread that has not called Looper.prepare()"
package com.example.kbr.utils; import android.view.Gravity;
import android.widget.Toast; import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; /**
* Created by keranbin on 2017/12/19.
*/ public class ToastUtil {
private static Toast toast; /**
* 短时间显示Toast【居上】
*
* @param msg 显示的内容-字符串
*/
public static void showShortToastTop(String msg) {
if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
Observable.create(emit -> { }).observeOn(AndroidSchedulers.mainThread())
.subscribe(re -> {
if (toast == null) {
toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
} else {
toast.setText(msg);
}
toast.show();
});
}
} /**
* 短时间显示Toast【居中】
*
* @param msg 显示的内容-字符串
*/
public static void showShortToastCenter(String msg) {
if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
io.reactivex.Observable.create(emit -> {
emit.onNext(msg);
emit.onComplete();
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(re -> {
if (toast == null) {
toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
// toast.setGravity(Gravity.CENTER,0,0); 默认居中显示
toast.show();
}); }
} /**
* 短时间显示Toast【居下】
*
* @param msg
*/
public static void showShortToast(String msg) {
if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
Observable.create(emit -> {
emit.onNext(msg);
emit.onComplete();
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(re -> {
if (toast == null) {
toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.show();
}); }
} /**
* 长时间显示Toast【居上】
*
* @param msg 显示的内容-字符串
*/
public static void showLongToastTop(String msg) {
if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
Observable.create(emit -> {
emit.onNext(msg);
emit.onComplete();
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(re -> {
if (toast == null) {
toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_LONG);
} else {
toast.setText(msg);
}
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
});
} } /**
* 长时间显示Toast【居中】
*
* @param msg 显示的内容-字符串
*/
public static void showLongToastCenter(String msg) {
if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
Observable.create(emit -> {
emit.onNext(msg);
emit.onComplete();
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(re -> {
if (toast == null) {
toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_LONG);
} else {
toast.setText(msg);
}
// toast.setGravity(Gravity.CENTER, 0, 0); //默认居中显示
toast.show();
});
} } /**
* 长时间显示Toast【居下】
*
* @param msg 显示的内容-字符串
*/
public static void showLongToast(String msg) {
if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
Observable.create(emit -> {
emit.onNext(msg);
emit.onComplete();
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(re -> {
if (toast == null) {
toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_LONG);
} else {
toast.setText(msg);
}
toast.setGravity(Gravity.BOTTOM, 0, 0); //默认居中显示
toast.show();
});
}
}
}
工具类ToastUtil 避免在子线程中使用抛异常 "Can't create handler inside thread that has not called Looper.prepare()"的更多相关文章
- 关于子线程使用Toast报错Can't create handler inside thread that has not called Looper.prepare()的解决办法
形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherD ...
- Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()
MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toStri ...
- 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- 【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误
原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...
- 在okhttp的callback回调中加Toast出现Cant create handler inside hread that has not called Looper.prepare()...
2019独角兽企业重金招聘Python工程师标准>>> 分析:callback中回调的response方法中还是在子线程中运行的,所以要调取Toast必须回到主线程中更新ui 解决方 ...
- android 不能在子线程中更新ui的讨论和分析
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...
- (转)Android在子线程中更新Activity中UI的方法
转:http://blog.sina.com.cn/s/blog_3fe961ae0100mvc5.html 在Android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处 ...
随机推荐
- gor实现线上HTTP流量复制压测引流
一.使用背景 gor 是一款go语言实现的简单的http流量复制工具,它的主要目的是使你的生产环境HTTP真实流量在测试环境和预发布环境重现.只需要在 代理例如nginx入口服务器上执行一个进程,就可 ...
- 利用Azure虚拟机安装Dynamics 365 Customer Engagement之九:新建组织
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- Cesium专栏-克里金插值(全国温度为例,附源码下载)
Cesium Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精 ...
- 20个常用的JavaScript字符串方法
摘要: 玩转JS字符串. 原文:JS 前20个常用字符串方法及使用方式 译者:前端小智 Fundebug经授权转载,版权归原作者所有. 本文主要介绍一些最常用的JS字符串函数. 1. charAt(x ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- 微服务、SpringCloud、k8s、Istio杂谈
一.微服务与SOA “微服务”是一个名词,没有这个名词之前也有“微服务”,一个朗朗上口的名词能让大家产生一个认知共识,这对推动一个事务的发展挺重要的,不然你叫微服务他叫小服务的大家很难集中到一个点上. ...
- .NET Core NuGet 多项目套餐打包的正确姿势
NuGet 默认只支持一个菜一个菜打包,不支持套餐打包.当对一个 csproj 项目进行 nuget 打包时(比如使用 dotnet pack 命令),只会将当前项目 build 出来的 dll 程序 ...
- CSharpGL(55)我是这样理解PBR的
CSharpGL(55)我是这样理解PBR的 简介 PBR(Physically Based Rendering),基于物理的渲染,据说是目前最先进的实时渲染方法.它比Blinn-Phong方法的真实 ...
- nodejs的require是如何执行的
通常,在Node.js里导入是通过 require函数调用进行的. Node.js会根据 require的是相对路径还是非相对路径做出不同的行为. 相对路径 相对路径很简单. 例如,假设有一个文件路径 ...
- Selenium+java - 通过Robot对象上传文件
思路: 1.将文件路径复制到剪切板 2.用robot对象模拟键盘操作即可 复制文件代码: public void setClipboardData(String data) { StringSelec ...