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. C# params 用法

    params 主要用在方法或函数参数数组中, 1,当参数个数不确定时使用 2,不能盒ref,和out组合使用 3,与参数数组对应的实参可以时一个 同类型数组,也可以时任意多个同类型变量 4,实参是数组 ...

  2. Python 第一個程序

    以默認方式安裝,會將 Python 安裝在目錄 C:\Users\Administrator\AppData\Local\Programs\Python\Python37 下: 有趣的是: 在此目錄下 ...

  3. [转]Introduction - Run Excel Macro using VBScript

    本文转自:https://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/ Have you ever ...

  4. 简单快速上手Jackson使用

    1简介 Jackson具有比较高的序列化和反序列化效率,据测试,无论是哪种形式的转换,Jackson > Gson > Json-lib,而且Jackson的处理能力甚至高出Json-li ...

  5. oracle常用查询sql

    oracle常用查询sql 原创 gordon陈 发布于2018-05-10 22:32:18 阅读数 297 收藏 展开 #!/bin/sh## create by Gordon Chen echo ...

  6. MySQL数据库:在命令提示符中使用mysql

    服务启动 在命令提示符中 启动mysql服务 net start mysql 停止mysql服务 net sotp mysql 通过命令行进入 mysql -u 用户名 -p 键入后会提示输入密码 如 ...

  7. How to recover a skipped tablespace after an incomplete recovery? (Doc ID 1561645.1)

    How to recover a skipped tablespace after an incomplete recovery? (Doc ID 1561645.1) APPLIES TO: Ora ...

  8. django之ORM字段及参数

    目录 ORM字段及参数 orm常用字段 字段合集 自定义char字段 字段参数 外键字段的参数 ORM字段及参数 orm常用字段 字段名 说明 AutoField 如果自己没有定义主键id,djang ...

  9. Scrapy 下载图片时 ModuleNotFoundError: No module named'PIL'

    使用scrapy的下载模块需要PIL(python图像处理模块)的支持,使用pip安装即可

  10. Acwing 14. 不修改数组找出重复的数字

    题目地址  https://www.acwing.com/problem/content/description/15/ 来源:剑指Offer 给定一个长度为 n+1n+1 的数组nums,数组中所有 ...