诀窍是跟踪显示的最后一个Toast,并取消那个.

我所做的是创建一个Toast包装器,它包含对显示的最后一个Toast的静态引用.

当我需要显示一个新的时,我首先取消静态引用,然后再显示新引用(并将其保存在静态中).

这是我制作的Boast包装器的完整代码 – 它模仿了Toast方法,足以让我使用它.默认情况下,Boast将取消前一个,因此您不会构建等待显示的Toasts队列.

这段代码可以在我的Github主旨中找到:

如果您只是想知道如何在退出应用时取消通知,那么您会在那里找到很多帮助.如果您有改进或建议,请随意分叉并取得联系.这是一个非常古老的答案,但代码在一些应用程序的生产中已经稳定了一段时间.

顺便说一句 – 在大多数用例中,这应该是Toast的直接替代品.

package mobi.glowworm.lib.ui.widget;

import android.annotation.SuppressLint;

import android.content.Context;

import android.content.res.Resources;

import android.support.annotation.Nullable;

import android.widget.Toast;

import java.lang.ref.WeakReference;

/**

* {@link Toast} decorator allowing for easy cancellation of notifications. Use this class if you

* want subsequent Toast notifications to overwrite current ones.

*

* By default, a current {@link Boast} notification will be cancelled by a subsequent notification.

* This default behaviour can be changed by calling certain methods like {@link #show(boolean)}.

*/

public class Boast {

/**

* Keeps track of certain Boast notifications that may need to be cancelled. This functionality

* is only offered by some of the methods in this class.

*

* Uses a {@link WeakReference} to avoid leaking the activity context used to show the original {@link Toast}.

*/

@Nullable

private volatile static WeakReference weakBoast = null;

@Nullable

private static Boast getGlobalBoast() {

if (weakBoast == null) {

return null;

}

return weakBoast.get();

}

private static void setGlobalBoast(@Nullable Boast globalBoast) {

Boast.weakBoast = new WeakReference<>(globalBoast);

}

//

/**

* Internal reference to the {@link Toast} object that will be displayed.

*/

private Toast internalToast;

//

/**

* Private constructor creates a new {@link Boast} from a given {@link Toast}.

*

* @throws NullPointerException if the parameter is null.

*/

private Boast(Toast toast) {

// null check

if (toast == null) {

throw new NullPointerException("Boast.Boast(Toast) requires a non-null parameter.");

}

internalToast = toast;

}

//

/**

* Make a standard {@link Boast} that just contains a text view.

*

* @param context The context to use. Usually your {@link android.app.Application} or

* {@link android.app.Activity} object.

* @param text The text to show. Can be formatted text.

* @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or

* {@link Toast#LENGTH_LONG}

*/

@SuppressLint("ShowToast")

public static Boast makeText(Context context, CharSequence text, int duration) {

return new Boast(Toast.makeText(context, text, duration));

}

/**

* Make a standard {@link Boast} that just contains a text view with the text from a resource.

*

* @param context The context to use. Usually your {@link android.app.Application} or

* {@link android.app.Activity} object.

* @param resId The resource id of the string resource to use. Can be formatted text.

* @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or

* {@link Toast#LENGTH_LONG}

* @throws Resources.NotFoundException if the resource can't be found.

*/

@SuppressLint("ShowToast")

public static Boast makeText(Context context, int resId, int duration)

throws Resources.NotFoundException {

return new Boast(Toast.makeText(context, resId, duration));

}

/**

* Make a standard {@link Boast} that just contains a text view. Duration defaults to

* {@link Toast#LENGTH_SHORT}.

*

* @param context The context to use. Usually your {@link android.app.Application} or

* {@link android.app.Activity} object.

* @param text The text to show. Can be formatted text.

*/

@SuppressLint("ShowToast")

public static Boast makeText(Context context, CharSequence text) {

return new Boast(Toast.makeText(context, text, Toast.LENGTH_SHORT));

}

/**

* Make a standard {@link Boast} that just contains a text view with the text from a resource.

* Duration defaults to {@link Toast#LENGTH_SHORT}.

*

* @param context The context to use. Usually your {@link android.app.Application} or

* {@link android.app.Activity} object.

* @param resId The resource id of the string resource to use. Can be formatted text.

* @throws Resources.NotFoundException if the resource can't be found.

*/

@SuppressLint("ShowToast")

public static Boast makeText(Context context, int resId) throws Resources.NotFoundException {

return new Boast(Toast.makeText(context, resId, Toast.LENGTH_SHORT));

}

//

/**

* Show a standard {@link Boast} that just contains a text view.

*

* @param context The context to use. Usually your {@link android.app.Application} or

* {@link android.app.Activity} object.

* @param text The text to show. Can be formatted text.

* @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or

* {@link Toast#LENGTH_LONG}

*/

public static void showText(Context context, CharSequence text, int duration) {

Boast.makeText(context, text, duration).show();

}

/**

* Show a standard {@link Boast} that just contains a text view with the text from a resource.

*

* @param context The context to use. Usually your {@link android.app.Application} or

* {@link android.app.Activity} object.

* @param resId The resource id of the string resource to use. Can be formatted text.

* @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or

* {@link Toast#LENGTH_LONG}

* @throws Resources.NotFoundException if the resource can't be found.

*/

public static void showText(Context context, int resId, int duration)

throws Resources.NotFoundException {

Boast.makeText(context, resId, duration).show();

}

/**

* Show a standard {@link Boast} that just contains a text view. Duration defaults to

* {@link Toast#LENGTH_SHORT}.

*

* @param context The context to use. Usually your {@link android.app.Application} or

* {@link android.app.Activity} object.

* @param text The text to show. Can be formatted text.

*/

public static void showText(Context context, CharSequence text) {

Boast.makeText(context, text, Toast.LENGTH_SHORT).show();

}

/**

* Show a standard {@link Boast} that just contains a text view with the text from a resource.

* Duration defaults to {@link Toast#LENGTH_SHORT}.

*

* @param context The context to use. Usually your {@link android.app.Application} or

* {@link android.app.Activity} object.

* @param resId The resource id of the string resource to use. Can be formatted text.

* @throws Resources.NotFoundException if the resource can't be found.

*/

public static void showText(Context context, int resId) throws Resources.NotFoundException {

Boast.makeText(context, resId, Toast.LENGTH_SHORT).show();

}

//

/**

* Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally

* have to call this. Normally view will disappear on its own after the appropriate duration.

*/

public void cancel() {

internalToast.cancel();

}

/**

* Show the view for the specified duration. By default, this method cancels any current

* notification to immediately display the new one. For conventional {@link Toast#show()}

* queueing behaviour, use method {@link #show(boolean)}.

*

* @see #show(boolean)

*/

public void show() {

show(true);

}

/**

* Show the view for the specified duration. This method can be used to cancel the current

* notification, or to queue up notifications.

*

* @param cancelCurrent true to cancel any current notification and replace it with this new

* one

* @see #show()

*/

public void show(boolean cancelCurrent) {

// cancel current

if (cancelCurrent) {

final Boast cachedGlobalBoast = getGlobalBoast();

if ((cachedGlobalBoast != null)) {

cachedGlobalBoast.cancel();

}

}

// save an instance of this current notification

setGlobalBoast(this);

internalToast.show();

}

}

如何在android程序使用toast,Android在退出应用程序时以及正在显示toast时...相关推荐

  1. Android:销毁所有的Activity退出应用程序几种方式

    Android:销毁所有的Activity退出应用程序几种方式 author:DRC工作室 我们都知道,Activity是存放在栈中.在默认的情况下(standard)Activity在栈中是以先进后 ...

  2. Android 关于长按back键退出应用程序的实现

    Android 关于长按back键退出应用程序的实现 最近在做一个Android上的应用,碰到一个问题就是如何实现长按back键退出应用程序.在网上查找了很多资料,发现几乎没有这样的实现,大部分在处理 ...

  3. 后退一步 小程序_微信小程序:如何利用navigateBack退出小程序

    在Android开发时,我们可以使用finish()或者结束进程等手段达到退出app的效果,但是,微信小程序则没有提供退出小程序的api,必须用户点击系统返回键或者点击顶部小程序固定的的退出按钮... ...

  4. 微信小程序开发——点击按钮退出小程序的实现

    微信小程序官方是没有提供退出的API的,但是在navigator这个组件中,是有退出这个功能的: 详情参考官方文档:navigator. 示例代码: 1 <navigator open-type ...

  5. Python 结束程序——如何在终端中退出 Python 程序

    您可以在终端中执行 Python 代码,就像在 VS Code.Atom 等 IDE 中一样.您可以在 Windows 和 Unix 操作系统(如 Linux 和 macOS)中执行此操作. 在本文中 ...

  6. Android两次按返回键退出应用程序

    关于这个问题,我以前并没有从网上参考别人的方法,自己想法又总是有些呆板.之前采用的方法是,设置一个int标志,0和1.按的时候判断是不是0,不是,加1,是,退出.这个低级错误真是惭愧.导致后来经过几次 ...

  7. 在Metro App中显示Toast notification

    Toast notification是向用户显示一些有关App的即时消息.具体Toast notification是什么可以参考  http://msdn.microsoft.com/en-us/li ...

  8. 怎样使用菜单栏中的Apple图标在苹果Mac上强制退出应用程序?

    如果 Mac 上的某个 App 停止响应,并且用户无法正常退出这个 App,使用"强制退出"来关闭这个 App.大家都知道Command + Option + Escape键盘快捷 ...

  9. PyQt5——退出应用程序和设置图标

    退出应用程序 import sys from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QPushButton,QWidg ...

最新文章

  1. 菜鸟学算法一基础知识篇
  2. 你不知道的console.log
  3. TiDB 在知乎万亿量级业务数据下的实践和挑战
  4. MYSQL:多表联合查询的例子
  5. sql server日期时间函数
  6. 精准扶贫电商重整流通-农业大健康·李玉庭: 谋定功能农产品
  7. 无法从套接字中获取更多数据_数据科学中应引起更多关注的一个组成部分
  8. javaweb引用serverlet库
  9. android+usb转串口+唯一id,Android平台3G模块驱动移植-USB转串口
  10. 电脑版微信怎么看朋友圈_电脑上也可以看朋友圈,99%的人都不知道!
  11. java学习路径1--转自byr
  12. mysql中为啥只显示一条语句_MySQL 笔记整理(19) --为什么我只查一行的语句,也执行这么慢?...
  13. nas 微型计算机,快速做种,PT上传更方便 用NAS玩转PT
  14. seo具体怎么优化-优化SEO的方法
  15. 英特尔oneAPI—开拓
  16. CCF201809-1 卖菜(JAVA)
  17. nvme固态硬盘开机慢_别让谣言害了你!关于固态硬盘的谣言以及使用误区 NVMe固态硬盘卡慢怎么办 NVMe固态硬盘卡慢解决方法【详解】-宝商在线...
  18. 7款免费发短信软件,看看哪一款适合你
  19. 招标流程及注意事项_资讯详情
  20. wps o ffice 2015个人版官方版 v9.10.4953 最新版​

热门文章

  1. 现行公路工程规范(2023)
  2. Github配置DNS
  3. win10系统要求配置_安装WinXP、Win7、Win8、Win10系统的最低电脑配置介绍
  4. 魔坊APP项目-26-直播、docker安装OSSRS流媒体直播服务器、基于APICloud的acLive直播推流模块实现RTMP直播推流、直播流管理
  5. QT遍历文件夹下的所有文件
  6. 操作系统找不到已输入的环境选项解决方案
  7. matlab用辛普森公式求积分_变限积分函数求导以及高阶导数求法的一些总结
  8. 辽宁省辽阳市谷歌高清卫星地图下载
  9. CoPuppy多重元素构建“DEFI+NFT”元宇宙
  10. 奶茶店冬天怎么提升销量 | 奶茶技术培训