IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

[Remote Service] Quel context pour afficher un toast ?!


Sujet :

Android

  1. #1
    Membre actif
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Points : 261
    Points
    261
    Par défaut [Remote Service] Quel context pour afficher un toast ?!
    Bonjour,

    je suis en train de mettre en place un service qui démarre au boot du mobile et effectue une tâche tout les X secondes.

    Je me retrouve donc avec un Timer prenant en paramètre un TimerTask qui surcharge la méthode run. Dans celle-ci je souhaite afficher un toast à chaque interval.

    J'ai donc crée une classe qui hérite de TimerTask et qui surcharge run(). Dans le constructeur de cette classe, je chope un context.
    Mais que je mette BackgroundService.this ou getApplicationContext() mes toast ne s'affiche pas ...

    Appel du constructeur depuis ma classe héritant de Service:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    @Override
    	public int onStartCommand(Intent intent,int flags,int startId){
    		Log.i("","## --- Service Started --- ##");
    		timer.scheduleAtFixedRate(new BackgroundServiceTimer(BackgroundService.this,db), 0, 10000);
    		return START_STICKY;
    	}
    Ici que je mette BackgroundService.this ou getApplciationContext c'est la même !

    Méthode Run surchargé :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    @Override
    	public void run() {
    		CharSequence text = "SMS launched";
    		int duration = Toast.LENGTH_LONG;
    		Toast toast = Toast.makeText(this.context, text, duration);
    		Log.i("TEST","// ------------------------------------------------------ //");
    		Log.i("TEST","//---------------------- TOAST ! -------------------------//");
    		Log.i("TEST","// ------------------------------------------------------ //");
    		toast.show();
    	}
    Ici les logs ne s'affiche pas quand je laisse le toast, si je l'enlève les Log s'affiche bien.

    Voilà j'arrête pas de bloquer sur des trucs qui je pense sont tout bête mais je ne perds pas espoir !

    merci d'avance !
    Paye Tes Dettes - Applciation android.

    DevHackSecure - Pense bête d'un étudiant en informatique - Tutos DEV

    " I also realize that _everybody_ thinks that they are right, and that they are supported by all other right-thinking people. That's just how people work. We all think we're better than average." Linus Torvalds

  2. #2
    Membre averti Avatar de _Xavier_
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    311
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Mai 2009
    Messages : 311
    Points : 390
    Points
    390
    Par défaut
    A toast is a view containing a quick little message for the user
    Si je ne me trompe les activités restent les seuls composants qui peuvent afficher une vue.

  3. #3
    Membre actif
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Points : 261
    Points
    261
    Par défaut
    Donc je ne peut pas avertir l'utilisateur si je n'est pas d'activity qui controle mon service ? Ca me parait un peu limité ... lol
    Paye Tes Dettes - Applciation android.

    DevHackSecure - Pense bête d'un étudiant en informatique - Tutos DEV

    " I also realize that _everybody_ thinks that they are right, and that they are supported by all other right-thinking people. That's just how people work. We all think we're better than average." Linus Torvalds

  4. #4
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    pour compléter ce qu'a dit Xavier

    A toast can be created and displayed from an Activity or Service. If you create a toast notification from a Service, it appears in front of the Activity currently in focus.

    If user response to the notification is required, consider using a Status Bar Notification.
    Donc on peut les créer à partir du service , si une activity est en cours .

    Donc je te conseille de passer par la barre de notification .

    http://developer.android.com/guide/t...fications.html
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  5. #5
    Membre actif
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Points : 261
    Points
    261
    Par défaut
    Ok merci, c'est ce que j'était en train d'essayer d'implémenter !

    Je tiens au jus !
    Paye Tes Dettes - Applciation android.

    DevHackSecure - Pense bête d'un étudiant en informatique - Tutos DEV

    " I also realize that _everybody_ thinks that they are right, and that they are supported by all other right-thinking people. That's just how people work. We all think we're better than average." Linus Torvalds

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    57
    Détails du profil
    Informations personnelles :
    Localisation : France, Nord (Nord Pas de Calais)

    Informations forums :
    Inscription : Novembre 2010
    Messages : 57
    Points : 57
    Points
    57
    Par défaut
    tu peux essayer dans partie : Context de Toast

    tonclass.this,
    getBaseContext(),
    tonClass.getContext(),


    etc... ca dépend le context que tu va utiliser avec Toast

  7. #7
    Membre actif
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Points : 261
    Points
    261
    Par défaut
    maydenec : justement le problème c'est que n'importe lequel ne fonctionne pas ... this fonctionne pas, maclass.this non plus, getApplicationContext() non plus, donc bon ça ne doit pas être possible si le service ne dispose pas d'activity (ce qui est mon cas).

    Pour les notifications, ça fonctionne bien mais est-ce normal que les notifications s'écrasent au lieu de m'en créer un nouvelle à chaque fois ? (c'est ce que je veux mais je me demande pourquoi ça n'écrase pas l'ancienne ? Est-ce à cause de l'id que l'on donne à l'ouverture et fermeture ?)
    Paye Tes Dettes - Applciation android.

    DevHackSecure - Pense bête d'un étudiant en informatique - Tutos DEV

    " I also realize that _everybody_ thinks that they are right, and that they are supported by all other right-thinking people. That's just how people work. We all think we're better than average." Linus Torvalds

  8. #8
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    Je pense que tu devrais passer par là ,
    Updating the notification

    You can update the information in your status bar notification as events continue to occur in your application. For example, when a new SMS text message arrives before previous messages have been read, the Messaging application updates the existing notification to display the total number of new messages received. This practice of updating an existing Notification is much better than adding new Notifications to the NotificationManager because it avoids clutter in the Notifications window.

    Because each notification is uniquely identified by the NotificationManager with an integer ID, you can revise the notification by calling setLatestEventInfo() with new values, change some field values of the Notification, and then call notify() again.

    You can revise each property with the object member fields (except for the Context and the expanded message title and text). You should always revise the text message when you update the notification by calling setLatestEventInfo() with new values for contentTitle and contentText. Then call notify() to update the notification. (Of course, if you've created a custom expanded view, then updating these title and text values has no effect.)
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  9. #9
    Membre actif
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Points : 261
    Points
    261
    Par défaut
    Merci
    Paye Tes Dettes - Applciation android.

    DevHackSecure - Pense bête d'un étudiant en informatique - Tutos DEV

    " I also realize that _everybody_ thinks that they are right, and that they are supported by all other right-thinking people. That's just how people work. We all think we're better than average." Linus Torvalds

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Gtkmm] Quel objet pour afficher des graphs
    Par smarties dans le forum GTK+ avec C & C++
    Réponses: 5
    Dernier message: 02/07/2011, 10h20
  2. Quel langage pour afficher les elements futurs
    Par pueblorasta dans le forum Débuter
    Réponses: 3
    Dernier message: 27/06/2011, 20h12
  3. Quel webpart pour afficher uniquement les sous-sites d'un site?
    Par gracouille dans le forum SharePoint
    Réponses: 0
    Dernier message: 02/06/2008, 19h37
  4. [Galerie] Quel script pour afficher des reportages photos ?
    Par silvain dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 4
    Dernier message: 04/12/2006, 14h29
  5. Quel composant pour afficher un graphique ?
    Par tipiweb dans le forum Composants VCL
    Réponses: 6
    Dernier message: 18/06/2005, 17h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo