2015-10-22 1 views
1

Titre semble question en double, mais ce n'est pas, croyez-moi.Annuler en train de montrer toast en raison d'une autre application dans android

Je veux annuler le toast actuellement affiché à l'écran en raison d'un autre service (je ne peux pas obtenir l'objet toast donc je ne peux pas appeler toast.cancel()).

Est-il possible d'obtenir des informations sur les toasts actuellement affichés à l'écran en raison d'une autre application/service, puis de l'annuler?

Toute aide serait grandement appréciée.

Merci beaucoup à l'avance.

+0

je voudrais ajouter un exemple pour plus de clarté. Disons que je cours APP 'A'. Je fais une opération qui appelle le service 'B' pour afficher le toast à l'écran. Maintenant, je veux annuler immédiatement ce pain grillé. Est-il possible d'afficher l'objet Toast à l'écran, quelle que soit l'application/le service qui affiche ce toast. –

Répondre

0

Il n'existe aucun moyen direct de le faire. Cependant, vous pouvez utiliser une classe wrapper pour Toast pour conserver la référence pour vous. Cela a été proposé par @Richard Le Mesurierhere. Tous les crédits vont à lui.

Vous pouvez essentiellement remplacer Toast avec Boast.


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

salut qmar! Merci d'avoir répondu. C'est d'accord. J'utilise déjà ce concept à ma manière bien sûr. Mais cette chose ne peut garder trace que de mes propres toasts. Mais je veux aussi obtenir des informations sur d'autres toasts. Mais cela ne semble pas possible d'obtenir un toast objet de toast en cours sur l'écran en raison d'une autre application. –

+0

J'ai aussi regardé les sources: j'ai essayé d'obtenir le message en utilisant findById(), et en l'obtenant aussi par le service interne. Pas de succès cependant. Je suppose qu'il y a une manière ultra-hachée d'utiliser la réflexion mais je ne la recommanderais pas, car la mise en œuvre interne peut toujours changer. –