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()"的更多相关文章

  1. 关于子线程使用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 ...

  2. 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 ...

  3. 在子线程中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 ...

  4. 【转】在子线程中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 ...

  5. 转 在子线程中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 ...

  6. 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报 ...

  7. 在okhttp的callback回调中加Toast出现Cant create handler inside hread that has not called Looper.prepare()...

    2019独角兽企业重金招聘Python工程师标准>>> 分析:callback中回调的response方法中还是在子线程中运行的,所以要调取Toast必须回到主线程中更新ui 解决方 ...

  8. android 不能在子线程中更新ui的讨论和分析

    问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...

  9. (转)Android在子线程中更新Activity中UI的方法

    转:http://blog.sina.com.cn/s/blog_3fe961ae0100mvc5.html 在Android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处 ...

随机推荐

  1. (转)格拉布斯准则(Grubbs Criterion)处理数据异常

    格拉布斯准则:https://baike.baidu.com/item/%E6%A0%BC%E6%8B%89%E5%B8%83%E6%96%AF%E5%87%86%E5%88%99/3909586 G ...

  2. javaWeb核心技术第九篇之JSP

    JSP:全名是Java Server Pages,它是建立在Servlet规范之上的动态网页开发技术.在JSP文件中,HTML代码与Java代码共同存在,其中,HTML代码用来实现网页中静态内容的显示 ...

  3. Add a Class from the Business Class Library从业务类库添加类(EF)

    In this lesson, you will learn how to use business classes from the Business Class Library as is. Fo ...

  4. Vue结合后台导入导出Excel问题详解

    话不多说,直接上前端代码 axios({ method: 'post', url: 'http://localhost:19090/exportUser',//这个是请求的地址 params: {// ...

  5. 如何搭建node - express 项目

    基于博主也是个菜鸟,亲身体验后步骤如下: 首先,我们需要安装node.js,  https://www.runoob.com/nodejs/nodejs-install-setup.html 安装完成 ...

  6. Pandas处理超大规模数据

    对于超大规模的csv文件,我们无法一下将其读入内存当中,只能分块一部分一部分的进行读取: 首先进行如下操作: import pandas as pd reader = pd.read_csv('dat ...

  7. Thymeleaf常用语法:数据延迟加载

    在处理模板时,可以由模板逻辑决定是否加载数据,以提高性能.在Spring Boot控制器中设置数据时,使用LazyContextVariable可以实现这功能. 开发环境:IntelliJ IDEA ...

  8. Spring Boot 2 单元测试

    开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8 IDEA新建一个Spring Boot项目后,pom.xml默认包含了Web应用和单元测试两个依赖包.如下 ...

  9. Android Studio如何配置CURL指令一键打包apk上传至蒲公英

    Android Studio如何配置CURL指令一键打包apk上传至蒲公英 第一步:在所需要打包的模块build.gradle文件中加入如下代码: android{ buildTypes { //配置 ...

  10. Linux:LNMP环境的搭建

    LNMP环境的搭建 安装DNS服务器 安装DNS服务 yum install bind -y DNS的配置 创建正向解析 以创建一个名为"lsy.com"的正向查找区域为例: 第一 ...