zoukankan      html  css  js  c++  java
  • 工具类ToastUtil 避免在子线程中使用抛异常 "Can't create handler inside thread that has not called Looper.prepare()"

      1 package com.example.kbr.utils;
      2 
      3 import android.view.Gravity;
      4 import android.widget.Toast;
      5 
      6 import io.reactivex.Observable;
      7 import io.reactivex.android.schedulers.AndroidSchedulers;
      8 
      9 /**
     10  * Created by keranbin on 2017/12/19.
     11  */
     12 
     13 public class ToastUtil {
     14     private static Toast toast;
     15 
     16 
     17     /**
     18      * 短时间显示Toast【居上】
     19      *
     20      * @param msg 显示的内容-字符串
     21      */
     22     public static void showShortToastTop(String msg) {
     23         if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
     24             Observable.create(emit -> {
     25 
     26             }).observeOn(AndroidSchedulers.mainThread())
     27                     .subscribe(re -> {
     28                         if (toast == null) {
     29                             toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_SHORT);
     30                             toast.setGravity(Gravity.TOP, 0, 0);
     31                         } else {
     32                             toast.setText(msg);
     33                         }
     34                         toast.show();
     35                     });
     36         }
     37     }
     38 
     39 
     40     /**
     41      * 短时间显示Toast【居中】
     42      *
     43      * @param msg 显示的内容-字符串
     44      */
     45     public static void showShortToastCenter(String msg) {
     46         if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
     47             io.reactivex.Observable.create(emit -> {
     48                 emit.onNext(msg);
     49                 emit.onComplete();
     50             }).observeOn(AndroidSchedulers.mainThread())
     51                     .subscribe(re -> {
     52                         if (toast == null) {
     53                             toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_SHORT);
     54                         } else {
     55                             toast.setText(msg);
     56                         }
     57 //                        toast.setGravity(Gravity.CENTER,0,0); 默认居中显示
     58                         toast.show();
     59                     });
     60 
     61         }
     62     }
     63 
     64 
     65     /**
     66      * 短时间显示Toast【居下】
     67      *
     68      * @param msg
     69      */
     70     public static void showShortToast(String msg) {
     71         if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
     72             Observable.create(emit -> {
     73                 emit.onNext(msg);
     74                 emit.onComplete();
     75             }).observeOn(AndroidSchedulers.mainThread())
     76                     .subscribe(re -> {
     77                         if (toast == null) {
     78                             toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_SHORT);
     79                         } else {
     80                             toast.setText(msg);
     81                         }
     82                         toast.setGravity(Gravity.BOTTOM, 0, 0);
     83                         toast.show();
     84                     });
     85 
     86 
     87         }
     88     }
     89 
     90 
     91     /**
     92      * 长时间显示Toast【居上】
     93      *
     94      * @param msg 显示的内容-字符串
     95      */
     96     public static void showLongToastTop(String msg) {
     97         if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
     98             Observable.create(emit -> {
     99                 emit.onNext(msg);
    100                 emit.onComplete();
    101             }).observeOn(AndroidSchedulers.mainThread())
    102                     .subscribe(re -> {
    103                         if (toast == null) {
    104                             toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_LONG);
    105                         } else {
    106                             toast.setText(msg);
    107                         }
    108                         toast.setGravity(Gravity.TOP, 0, 0);
    109                         toast.show();
    110                     });
    111         }
    112 
    113     }
    114 
    115 
    116     /**
    117      * 长时间显示Toast【居中】
    118      *
    119      * @param msg 显示的内容-字符串
    120      */
    121     public static void showLongToastCenter(String msg) {
    122         if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
    123             Observable.create(emit -> {
    124                 emit.onNext(msg);
    125                 emit.onComplete();
    126             }).observeOn(AndroidSchedulers.mainThread())
    127                     .subscribe(re -> {
    128                         if (toast == null) {
    129                             toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_LONG);
    130                         } else {
    131                             toast.setText(msg);
    132                         }
    133 //                        toast.setGravity(Gravity.CENTER, 0, 0);  //默认居中显示
    134                         toast.show();
    135                     });
    136         }
    137 
    138     }
    139 
    140     /**
    141      * 长时间显示Toast【居下】
    142      *
    143      * @param msg 显示的内容-字符串
    144      */
    145     public static void showLongToast(String msg) {
    146         if (EmptyUtil.isNotEmpty(msg) && EmptyUtil.isNotEmpty(App.getContext())) {
    147             Observable.create(emit -> {
    148                 emit.onNext(msg);
    149                 emit.onComplete();
    150             }).observeOn(AndroidSchedulers.mainThread())
    151                     .subscribe(re -> {
    152                         if (toast == null) {
    153                             toast = Toast.makeText(App.getContext(), msg, Toast.LENGTH_LONG);
    154                         } else {
    155                             toast.setText(msg);
    156                         }
    157                         toast.setGravity(Gravity.BOTTOM, 0, 0);  //默认居中显示
    158                         toast.show();
    159                     });
    160         }
    161     }
    162 }
  • 相关阅读:
    golang学习----nil值
    CentOS配置multipath
    oracle基础-创建表空间
    oracle数据库基本命令
    CentOS使用fdisk扩展磁盘空间
    CentOS增加swap分区
    VMWARE错误-"VirtualInfrastructure.Utils.ClientsXml"的类型初始值设定项引发异常
    windows server 2008远程桌面最大连接数设置
    windows sserver 2008远程桌面端口修改
    iSCSI配置
  • 原文地址:https://www.cnblogs.com/bdsdkrb/p/8079331.html
Copyright © 2011-2022 走看看