2015-10-22 22 views
1

中的一些其他應用程序的烤麪包標題似乎是重複的問題,但它不是,相信我。取消當前顯示由於android

我想取消當前在屏幕上顯示的toast由於其他服務(我不能得到烤麪包對象,所以不能撥打toast.cancel())。

有沒有什麼方法可以獲取當前屏幕上由於任何其他應用程序/服務而顯示的烤麪包的信息,然後取消它。

任何幫助將不勝感激。

很多人提前感謝。

+0

我想添加爲了更清楚的例子。可以說我正在運行APP'A'。我做了一些調用服務'B'來在屏幕上顯示烤麪包的操作。現在我想立即取消那個吐司。有沒有什麼辦法可以讓Toast對象在屏幕上顯示,而不管顯示哪個應用/服務的吐司。 –

回答

0

沒有直接的方法來做到這一點。但是,您可以使用Toast的包裝類來保留參考。這是由@Richard Le Mesurierhere提出的。所有學分都歸他所有。

您基本上可以代替ToastBoast


package com.mobiRic.ui.widget; 

import android.annotation.SuppressLint; 
import android.content.Context; 
import android.content.res.Resources; 
import android.widget.Toast; 

/** 
* {@link Toast} decorator allowing for easy cancellation of notifications. Use 
* this class if you want subsequent Toast notifications to overwrite current 
* ones. </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 {@link Boast} notifications that may need to be cancelled. 
    * This functionality is only offered by some of the methods in this class. 
    */ 
    private volatile static Boast globalBoast = null; 

    // //////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /** 
    * 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 #LENGTH_SHORT} or 
    *  {@link #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 #LENGTH_SHORT} or 
    *  {@link #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 #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 #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 #LENGTH_SHORT} or 
    *  {@link #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 #LENGTH_SHORT} or 
    *  {@link #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 #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 #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 && (globalBoast != null)) 
     { 
      globalBoast.cancel(); 
     } 

     // save an instance of this current notification 
     globalBoast = this; 

     internalToast.show(); 
    } 

} 
+0

嗨qmar!感謝回覆。沒關係。當然,我已經以自己的方式使用了這個概念。但這件事只能追蹤我自己的敬酒。但我也想得到其他敬酒的信息。但是,由於其他應用程序的原因,目前在屏幕上顯示敬酒的烤麪包對象似乎不太可能。 –

+0

我也查看了源代碼:試圖通過使用findById()獲取消息,並通過內部服務獲取消息。雖然沒有成功。我想有一種使用反射的非常冒險的方式,但我不會建議這樣做,因爲內部實現總是可以改變的。 –