这是我从另一个类似问题中复制的答案:
该Boast
课程完全满足您的需求。最新的代码可以在 GitHub 上找到:
诀窍是跟踪显示的最后一个Toast
,并取消那个。
我所做的是创建一个Toast
包装器,其中包含对显示的最后一个 Toast 的静态引用。
当我需要显示一个新的时,我首先取消静态引用,然后再显示新的(并将其保存在静态中)。
这是我制作的包装器的完整代码Boast
——它模仿了足够多的 Toast 方法供我使用。默认情况下,Boast
将取消前一个,因此您不会建立等待显示的 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. </p>
* <p/>
* 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.
* <p>
* Uses a {@link WeakReference} to avoid leaking the activity context used to show the original {@link Toast}.
*/
@Nullable
private volatile static WeakReference<Boast> 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 <code>null</code>.
*/
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 <code>true</code> 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();
}
}