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 :

Lancer une notification depuis un service android


Sujet :

Android

  1. #1
    Membre émérite
    Avatar de azstar
    Homme Profil pro
    Architecte Technique BizTalk/.NET
    Inscrit en
    Juillet 2008
    Messages
    1 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Architecte Technique BizTalk/.NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 198
    Points : 2 424
    Points
    2 424
    Par défaut Lancer une notification depuis un service android
    Bonjour;

    je cherche a réaliser un service qui envoi une notification pour ouvrir une activity dans mon application ?

    je ne sais où commencer ??


    merci d'avance

  2. #2
    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,

    Déjà un tutoriel sur les notifications :
    http://a-renouard.developpez.com/tut...notifications/

    je cherche a réaliser un service qui envoi une notification pour ouvrir une activity dans mon application ?
    Quel genre de service ?

    Pour ouvrir une activity depuis un notification c'est le même appel que dans une activity normale. Après peut être qu'il faudra que tu regardes les options des activity depuis le manifest si tu ne veux pas de doublon( sur tes activity) par exemple.
    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.

  3. #3
    Membre émérite
    Avatar de azstar
    Homme Profil pro
    Architecte Technique BizTalk/.NET
    Inscrit en
    Juillet 2008
    Messages
    1 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Architecte Technique BizTalk/.NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 198
    Points : 2 424
    Points
    2 424
    Par défaut
    Merci;

    J'ai réussi de réaliser ce que je vous

    voila le code de service

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
     
    package Android.App;
     
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
     
    import Android.App.R;
    import Android.App.Traitement.GlobalContextTraitement;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.provider.MediaStore.Audio;
    import android.util.Log;
     
    public class MyPharmacyBackgroundService extends Service { 
     
    private Timer timer ; 
    public static  int ID_NOTIFICATION = 1988;
     
     
    @Override
    public void onCreate() { 
    	//android.os.Debug.waitForDebugger();
        super.onCreate(); 
        timer = new Timer(); 
        Log.d(this.getClass().getName(), "onCreate"); 
    } 
     
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     
        Log.d(this.getClass().getName(), "onStart"); 
        timer.scheduleAtFixedRate(new TimerTask() { 
            public void run() { 
                // Executer de votre tâche 
            	Log.d("DDD", "Fisrt");
           List<Entity.NotificationInfo> infos=new NotificationClass(getApplicationContext()).GetNotifications();
     
           if (infos ==null)
        	   return;
           Log.d("DDD",String.valueOf(infos.size()));
           for(Entity.NotificationInfo info:infos)
            	createNotify(info);
     
            } 
        }, 0, 60000); //3600000
     
       return START_NOT_STICKY;
      // return  super.onStartCommand(intent, flags, startId);
    } 
     
    @Override 
    public void onDestroy() { 
        Log.d(this.getClass().getName(), "onDestroy"); 
        this.timer.cancel(); 
    }
     
    @Override
    public IBinder onBind(Intent intent) {
    	// TODO Auto-generated method stub
    	return null;
     } 
     
    //Méthode qui créer la notification
    private void createNotify(Entity.NotificationInfo  info){
    	//On créer un "gestionnaire de notification"
    	NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);        
     
    	//On créer la notification
    	//Avec son icône et son texte défilant (optionel si l'on veut pas de texte défilant on met cet argument à null)
    	Notification notification = new Notification(R.drawable.traitement, "MyPH@RM@CY : Vous avez un traitement a prendre !", System.currentTimeMillis());  
     
    	//Le PendingIntent c'est ce qui va nous permettre d'atteindre notre deuxième Activity
    	//ActivityNotification sera donc le nom de notre seconde Activity
    	  Intent intent= new Intent(GlobalContextTraitement.context, NotificationActivity.class);
    	  Bundle bundle = new Bundle();
    	    bundle.putString("notificaion", info.GetSerialeValue());
    	    bundle.putInt("NotifictionID",ID_NOTIFICATION);
    	    intent.putExtras(bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(GlobalContextTraitement.context, 0, intent, 0);
     
        //On définit le titre de la notif
        String titreNotification = "MyPH@RM@CY Notification !";
        //On définit le texte qui caractérise la notif
        String texteNotification = "Vous avez un medicament a prendre...";         
     
        //On configure notre notification avec tous les paramètres que l'on vient de créer
        notification.setLatestEventInfo(this, titreNotification, texteNotification, pendingIntent);
        //On ajoute un style de vibration à notre notification
        //L'utilisateur est donc également averti par les vibrations de son téléphone
        //Ici les chiffres correspondent à 0sec de pause, 0.2sec de vibration, 0.1sec de pause, 0.2sec de vibration, 0.1sec de pause, 0.2sec de vibration
        //Vous pouvez bien entendu modifier ces valeurs à votre convenance
        if (info.isIsVibreur())
        notification.vibrate = new long[] {0,200,100,200,100,200};
        if (info.isIsSonnerie())
        notification.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "2");
     
     
        //Enfin on ajoute notre notification et son ID à notre gestionnaire de notification
        notificationManager.notify(ID_NOTIFICATION, notification);
       // ID_NOTIFICATION++; pour avoir plusieurs notification veuillez increment ID_NOTIFICATION
    }
     
    //Méthode pour supprimer de la liste de notification la notification que l'on vient de créer
     
    }

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

Discussions similaires

  1. Comment lancer une application depuis un service ?
    Par bhf37 dans le forum Langage
    Réponses: 7
    Dernier message: 05/03/2009, 14h05
  2. Lancer une appli depuis un service windev
    Par vexal dans le forum WinDev
    Réponses: 2
    Dernier message: 04/03/2009, 11h22
  3. Réponses: 3
    Dernier message: 31/08/2008, 16h33
  4. Réponses: 9
    Dernier message: 11/01/2007, 21h23
  5. Lancer une action depuis une autre action
    Par anaon dans le forum Struts 1
    Réponses: 6
    Dernier message: 04/08/2006, 19h38

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