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. Selenium(一):原理与安装、简单的使用

    1. selenium原理 1.1 selenium介绍 Selenium是一个Web应用的自动化框架. 通过它,我们可以写出自动化程序,像人一样在浏览器里操作web界面. 比如点击界面按钮,在文本框 ...

  2. Swoole中内置Http服务器

    创建httpServer.php文件,代码如下: <?php // 创建服务对象 $http = new swoole_http_server("10.211.55.17", ...

  3. zabbix监控服务

    1.模板的重要 ​ 1.手动添加监控比较麻烦,监控项 -> 图形 -> 触发器. 问题: 1.例如: 100台服务器需要检查81端口 2.例如: 100台服务器81改成82 解决: 使用模 ...

  4. windows 应急流程及实战演练

    前言 本文摘自信安之路公众号的文章. 当企业发生黑客入侵.系统崩溃或其它影响业务正常运行的安全事件时,急需第一时间进行处理,使企业的网络信息系统在最短时间内恢复正常工作,进一步查找入侵来源,还原入侵事 ...

  5. swift多线程定时器

    swift多线程定时器的使用 func countDown(_ timeOut:Int,view: UIView){ var timeout = timeOut let queue:DispatchQ ...

  6. aws创建ec2虚拟机

    利用亚马逊AWS搭建个人服务器 https://www.jianshu.com/p/a045d4217175 https://segmentfault.com/a/1190000019201071?u ...

  7. 038.[转] JVM启动过程与类加载

    From: https://blog.csdn.net/luanlouis/article/details/40043991 Step 1.根据JVM内存配置要求,为JVM申请特定大小的内存空间 ? ...

  8. 如何在mac版本的python里安装pip

    mac里面python自带easy_install,在终端里面执行sudo easy_install pip.运行完可以用pip help测试一下是否安装成功,成功安装后,直接pip install ...

  9. Django—开发具体流程

    1.创建Django项目 [root@localhost ~]# django-admin startproject 项目名 [root@localhost ~]# django-admin star ...

  10. 使用docker运行springboot项目

    本文主要讲的是使用docker运行springboot项目 获取一个springboot项目 这里我没有重新构建,用的之前写的一个项目,直接从github上下载下来,地址:https://github ...