工具类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平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处 ...
 
随机推荐
- sql server 2014 卸载
			
遇到一个沙雕工程人员~二话不装给我装了2014的版本,实际开发的时候用的是2012....欸~ 1.打开服务 2.打开控制面板下的程序与功能 3选中红框点击卸载与更改----->选择删除 4.然 ...
 - 如何在CAD图纸中进行线性标注
			
在CAD中,都会在图纸中进行CAD标注,一般都是有CAD标注样式.CAD标注文字等.那其中有一个就是CAD线性标注?可以标注图纸间的距离?那如何在CAD图纸中进行线性标注呢?具体要怎么来进行操作?本篇 ...
 - OpenCV:图像的颜色空间转换
			
导包: import numpy as np import cv2 import matplotlib.pyplot as plt def show(image): plt.imshow(image) ...
 - Linux iotop工具简介
			
iotop的简介: iotop是一款开源.免费的用来监控磁盘I/O使用状况的类似top命令的工具,iotop可以监控进程的I/O信息.它是Python语言编写的,与iostat工具比较,iostat是 ...
 - Linux下磁盘实战操作命令
			
企业真实场景由于硬盘常年大量读写,经常会出现坏盘,需要更换硬盘.或者由于磁盘空间不足,需添加新硬盘,新添加的硬盘需要经过格式化.分区才能被 Linux 系统所使用. 虚拟机 CentOS 7 Linu ...
 - CentOS安装docker-compose
			
一.compose简介 compose是一个定义和运行多容器的docker应用的工具.compose 通过yaml文件配置应用服务,然后仅需一个命令就可以创建和运行所有配置中的服务. 二.compos ...
 - Modbus 协议
			
转载:https://www.cnblogs.com/DreamRecorder/p/9081127.html 一.Modbus 协议简介 Modbus 协议是应用于电子控制器上的一种通用语言 ...
 - spring mvc 源码简要分析
			
关于web项目,运用比较多的是过滤器和拦截器 过滤器基于责任链设计模式 创建过滤器链 / Create the filter chain for this requestApplicationFilt ...
 - day90_11_12
			
一.路由与正则. 1.当函数中需要参数,而不在路由中匹配的时候需要使用default方法: @app.route('/index/<testreg("\d+"):nid> ...
 - ACWING 95 费解的开关 解题记录
			
你玩过“拉灯”游戏吗?25盏灯排成一个5x5的方形.每一个灯都有一个开关,游戏者可以改变它的状态.每一步,游戏者可以改变某一个灯的状态.游戏者改变一个灯的状态会产生连锁反应:和这个灯上下左右相邻的灯也 ...