android 在多线程中运用的Toast
来源:互联网 发布:淘宝德国进口净水器 编辑:程序博客网 时间:2024/06/05 23:42
public class ToastUtils { /** * 可以在多线程里运行的toast */ private static volatile Toast mToast; private static final Object lock = new Object(); private static Context context; public static void init(Context con){ context = con; } public static void initToast(String msg) { initToast(msg, context, true); } public static void initToast(int resId, Context context) { if (context instanceof Activity || context instanceof FragmentActivity) context = context.getApplicationContext(); initToast(context.getString(resId), context, true); } public static void initToast(String msg, Context context, boolean isSingleton) { if (context instanceof Activity || context instanceof FragmentActivity) context = context.getApplicationContext(); if (mToast != null && isSingleton) { mToast.setText(msg); } else { synchronized (lock) { mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT); } } mToast.setGravity(Gravity.CENTER, 0, 0);//默认显示位置 mToast.show(); } //可以设置toast的位置 public static void setGravity(int gravity, int xOffset, int yOffset) { mToast.setGravity(gravity, xOffset, yOffset); } //可以自定义toast的view public void setView(View view) { mToast.setView(view); } /** 当你在线程中使用toast时,请使用这个方法(可以控制显示多长时间) */ public static void showInThread(@NonNull final Context context, final String msg, final int length) { new Thread() { @Override public void run() { Looper.prepare();//先移除 Toast.makeText(context, msg, length).show(); Looper.loop();// 进入loop中的循环,查看消息队列 } }.start(); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
下面的功能可以控制Toast显示时间,若是不需要控制 仅需上面的代码即可,下面的代码可忽略!
//////////////////////////////////////////////////////////////////////////////////////////////////////
/** * 以下全部代码为一个整体,可以控制显示时间的Toast */ private static Handler mHandler = null; private static int duration = 0; private static int currDuration = 0; private static final int DEFAULT = 2000; public static void showByDuration(Context context, String msg, int duration) { duration = duration; currDuration = DEFAULT; mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG); mHandler = new Handler(context.getMainLooper()); mHandler.post(mToastThread); } private static Runnable mToastThread = new Runnable() { public void run() { mToast.show(); mHandler.postDelayed(mToastThread, DEFAULT);// 每隔2秒显示一次 if (duration != 0) { if (currDuration <= duration) { currDuration += DEFAULT; } else { cancel(); } } } }; private static void cancel() { mHandler.removeCallbacks(mToastThread);// 先把显示线程删除 mToast.cancel();// 把最后一个线程的显示效果cancel掉,就一了百了了 currDuration = DEFAULT; }}
阅读全文
0 0
- android 在多线程中运用的Toast
- Android中Toast的使用
- Android中Toast的用法
- Android中Toast的使用
- Android 中Toast的使用
- Android中Toast的使用
- Android中Toast的使用
- 在Android中运用RxJava
- 在Android中使用Toast进行提示
- [Android] 在Service中使用Toast
- android 在service中使用toast
- Android:在Service中使用Toast
- Android Studio 在活动中使用Toast
- android Toast运行在主线程中
- Android 中toast显示在不同位置
- Android Toast工具类 解决Toast重复显示,Toast一直显示,在线程中可用
- Delphi中多线程中Synchronize的运用
- 从项目中谈JAVA中static 方法在多线程环境下的运用
- Python(16):所谓的“函数指针”
- 位置参数
- 内存分配—malloc()和free()
- 学习资源,干货!!!JAVA大全
- 操作系统处理机调度及常见的调度算法
- android 在多线程中运用的Toast
- 数据库的基本操作
- Androidstudio使用笔记
- 查看jdk的版本以及路径
- jQuey在MyEclipse中不起作用的解决方法
- Sort List (Frequent++)
- 简单抓站的N种方式-urllib与bs4
- 通过GlobalMemoryStatus函数获取当前虚拟内存和物理内存信息
- LeetCode 171. Excel Sheet Column Number