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 :

Désactiver Alarme une fois l'heure programmée passée


Sujet :

Android

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 61
    Points : 41
    Points
    41
    Par défaut Désactiver Alarme une fois l'heure programmée passée
    Bonjour à tous,

    Dans mon application, je souhaite porgrammer une notification à une date précise. Voici donc le code que j'utilise, d'après un tutorial que j'ai suivi:

    AlarmReceiver.java

    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
     
     
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
     
    public class AlarmReceiver extends BroadcastReceiver {
     
         @Override
         public void onReceive(Context context, Intent intent) {
         // When our Alaram time is triggered , this method will be excuted (onReceive)
         // We're invoking a service in this method which shows Notification to the User
          Intent myIntent = new Intent(context, NotificationService.class);
          context.startService(myIntent);
        }
     
    }
    NotificationService.java

    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
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
     
    public class NotificationService extends Service {
     
    	private NotificationManager mManager;
     
    	@Override
    	public IBinder onBind(Intent arg0) {
    		// TODO Auto-generated method stub
    		return null;
    	}
     
    	@Override
    	public void onCreate() {
    		super.onCreate();
    	}
     
    	@Override
    	public void onStart(Intent intent, int startId) {
    		super.onStart(intent, startId);
    		// Getting Notification Service
    		mManager = (NotificationManager) this.getApplicationContext()
    				.getSystemService(
    						this.getApplicationContext().NOTIFICATION_SERVICE);
    		/*
    		 * When the user taps the notification we have to show the Home Screen
    		 * of our App, this job can be done with the help of the following
    		 * Intent.
    		 */
    		Intent intent1 = new Intent(this.getApplicationContext(), MyView.class);
     
    		Notification notification = new Notification(R.drawable.ic_launcher,
    				"Message de la notification", System.currentTimeMillis());
     
    		intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
    				| Intent.FLAG_ACTIVITY_CLEAR_TOP);
     
    		PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
    				this.getApplicationContext(), 0, intent1,
    				PendingIntent.FLAG_UPDATE_CURRENT);
     
    		notification.flags |= Notification.FLAG_AUTO_CANCEL;
     
    		notification.setLatestEventInfo(this.getApplicationContext(),
    				"Message", "Message de la notification",
    				pendingNotificationIntent);
     
    		mManager.notify(0, notification);
    	}
     
    	@Override
    	public void onDestroy() {
    		// TODO Auto-generated method stub
    		super.onDestroy();
    	}
     
    }
    MyView.java (Activité principale)

    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
    import java.util.Calendar;
     
    import android.app.Activity;
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.os.Bundle;
     
    public class MyView extends Activity {
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
     
    		Calendar Calendar_Object = Calendar.getInstance();
    		Calendar_Object.set(Calendar.MONTH, 4);
    		Calendar_Object.set(Calendar.YEAR, 2013);
    		Calendar_Object.set(Calendar.DAY_OF_MONTH, 8);
     
    		Calendar_Object.set(Calendar.HOUR_OF_DAY, 12);
    		Calendar_Object.set(Calendar.MINUTE, 02);
    		Calendar_Object.set(Calendar.SECOND, 0);
     
     
    		// MyView is my current Activity, and AlarmReceiver is the
    		// BoradCastReceiver
    		Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
     
    		PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
    				0, myIntent, 0);
     
    		AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
     
    		/*
    		 * The following sets the Alarm in the specific time by getting the long
    		 * value of the alarm date time which is in calendar object by calling
    		 * the getTimeInMillis(). Since Alarm supports only long value , we're
    		 * using this method.
    		 */
     
    		alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),
    				pendingIntent);
    	}
    Et l'ajout de ceci dans mon manifest :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    <receiver android:name=".AlarmReceiver" >
            </receiver>
     
            <service
                android:name="NotificationService"
                android:enabled="true" />
    La notification se lance bien à la date et l'heure prévue. En revanche, une fois que l'heure est passée, lorsque je relance mon application, la notification se lance à nouveau.

    Comme puis-je faire alors pour que la notification se désactive dès que l'heure que j'ai programmé est passée?

    Je vous remercie par avance pour votre aide

  2. #2
    Membre habitué
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2011
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2011
    Messages : 216
    Points : 139
    Points
    139
    Par défaut
    bonjour,

    serait' il possible d'avoir le lien vers le tuto ? pars que j'ai du mal a comprendre ton code.

    tiens au jus.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 61
    Points : 41
    Points
    41
    Par défaut
    Tout d'abord, je m'excuse de répondre aussi tardivement et je te remercie beaucoup Yrtera pour m'avoir répondu.

    Mon problème n'est à ce jour toujours pas résolu.

    Voici le lien vers le tutorial que j'ai suivi : http://sankarganesh-info-exchange.bl...oid-using.html

    Merci d'avance.

  4. #4
    Membre habitué
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2011
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2011
    Messages : 216
    Points : 139
    Points
    139
    Par défaut
    salut,

    le problème vient du fait que lorsque tu lance ton appli, il crée ton alarme, comme la date est passée, il la lance de suite.

    tu peux vérifier si la date est passé, si elle n'est pas passé, tu crée ton alarme:

    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    	Calendar Calendar_Object = Calendar.getInstance();
     
    	Date now = Calendar_Object.getTime(); //recupere la date et l'heure actuelle en milliseconde
     
    	Calendar_Object.set(Calendar.MONTH, 4); //defini la date a laquelle lancer ton alarme
    	Calendar_Object.set(Calendar.YEAR, 2013); //idem
    	Calendar_Object.set(Calendar.DAY_OF_MONTH, 8); //idem
    	Calendar_Object.set(Calendar.HOUR_OF_DAY, 12); //idem
    	Calendar_Object.set(Calendar.MINUTE, 02); //idem
    	Calendar_Object.set(Calendar.SECOND, 0); // idem
     
    	if (Calendar_Object.getTime().before(now)) // si ta date n'est pas passée, tu lance ton alarme.
    		alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),pendingIntent);

    voilou, tiens au jus.

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 61
    Points : 41
    Points
    41
    Par défaut
    Merci beaucoup pour ta réponse rapide

    Je teste ça ce week end

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 61
    Points : 41
    Points
    41
    Par défaut
    J'ai modifié le "before" par "after" puisque l'alarme doit se lancer si l'heure vient après l'heure actuelle et miracle... ça fonctionne!!!! L'alarme se lance bien seulement à l'heure précise programmée.

    Merci beaucoup pour ton aide

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

Discussions similaires

  1. [XL-2013] Comment éxecuter des macros automatiquement une fois par heure ?
    Par Dan.exe dans le forum Macros et VBA Excel
    Réponses: 10
    Dernier message: 31/01/2014, 16h41
  2. supprimer des données une fois la date passée
    Par Bazooka dans le forum Access
    Réponses: 2
    Dernier message: 09/02/2007, 21h39
  3. Réponses: 2
    Dernier message: 26/01/2007, 15h28
  4. Mon script passe une fois de trop dans la boucle
    Par Stessy dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 08/08/2006, 11h57

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