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éclenchement alarme répétitive


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    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
    Par défaut Déclenchement alarme répétitive
    bonjour,

    j'ai crée une application pour modifier le mode du téléphone (vibreur, sonnerie, silencieux).

    j'ai utilisé des alarmes répétitives a intervale journalié dont voici le code:

    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    this.active = true;
        	this.intent = new Intent(context,alarme_action.class);
    		this.intent.setAction(type);
    		this.pending_intent = PendingIntent.getBroadcast(context, 0, this.intent, 0);
    		this.alarme_manager.set(AlarmManager.RTC_WAKEUP, this.date.getTimeInMillis(), this.pending_intent);

    le problème est que certaines fois, la pending intent ne se déclenche pas.

    Dans mon application il y a deux alarmes, la première alarme se déclenche tous les jour parfaitement, mais certaines fois la deuxième ne fait rien.

    ma question: y a t'il un journal des erreur ou je peux voir pourquoi l'alarme ne s'est pas déclenché ? ou avez vous une idée d'où peux venir le problème ?

    Merci.

  2. #2
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    Il semble que la l'alarme soit faite pour ne fonctionner qu'une seule fois... et n'est pas en mode répétition (setRepeating), ou n'a t-on qu'une petite partie du code ?

  3. #3
    Membre confirmé
    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
    Par défaut
    Ho oui, pardon, je me suis trompé de code.

    voila le code:

    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    this.active = true;
    		this.intent.setAction(type);
    		this.pending_intent = PendingIntent.getBroadcast(context, 0, this.intent, 0);
    long interval_defaut = AlarmManager.INTERVAL_DAY;
    this.alarme_manager.setRepeating(AlarmManager.RTC_WAKEUP, this.date.getTimeInMillis(), this.intervale, this.pending_intent);

    voila, Merci.

  4. #4
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    Et le code de la deuxième alarme ?

  5. #5
    Membre confirmé
    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
    Par défaut
    bonjour,

    c'est le même code, je sais pas si je peux le faire comme ça (ou si c'est de la chance que ça marche des fois.)

    j'ai crée un objet alarme dans lequel je crée mes alarmes:

    création des objets:

    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    		alarme_debut = new alarme_repetitive("alarme debut", false, AlarmManager.INTERVAL_DAY, this);
    		alarme_fin = new alarme_repetitive("alarme fin", false, AlarmManager.INTERVAL_DAY, this);
    		alarme_debut.alarme_manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    		alarme_fin.alarme_manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    puis la classe de l'objet:

    Code java : 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
    package com.bibi.chuut;
     
    import android.app.Activity;
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
     
    public class alarme_repetitive extends alarme{
     
    	protected long interval_defaut = AlarmManager.INTERVAL_DAY;
    	protected boolean inexact_defaut = false;
     
    	long intervale;
    	boolean inexact;
     
    	public long getintervale() {
            return intervale;
        }
        public void setintervale(long _intervale) {
            this.intervale = _intervale;
        }
     
    	public boolean getinexact() {
            return inexact;
        }
        public void setinexact(boolean _inexact) {
            this.inexact = _inexact;
        }
     
     
    	public alarme_repetitive(String _nom) {
    		super(_nom);
    		this.intervale = interval_defaut;
    		this.inexact = inexact_defaut;
    	}
     
    	public alarme_repetitive(String _nom, boolean _inexact, long _intervale,Context context) {
    		super(_nom);		
    		this.intervale=_intervale;
    		this.inexact = _inexact;
     
    		this.intent = new Intent(context,alarme_action.class);
    		this.type = type_defaut;
    		this.intent.setAction(type);
    	}
     
     
        public void activer(Context context)
        {		
    		this.active = true;
    		this.intent.setAction(type);
    		this.pending_intent = PendingIntent.getBroadcast(context, 0, this.intent, 0);
     
        	if(!this.inexact)
        	{
        		setRepeating(context);
        	}
        	else
        	{
        		setInexactRepeating(context);
        	}
     
        	enregistrement(context);
        }
     
     
    	private void setRepeating(Context context)
    	{
    		this.alarme_manager.setRepeating(AlarmManager.RTC_WAKEUP, this.date.getTimeInMillis(), this.intervale, this.pending_intent);
    	}
     
    	private void setInexactRepeating(Context context)
    	{
    		this.alarme_manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, this.date.getTimeInMillis(), this.intervale, this.pending_intent);
    	}
     
     
    	protected void enregistrement(Context context)
    	{
    		SharedPreferences donnees = context.getSharedPreferences(this.nom, Activity.MODE_PRIVATE);
    		SharedPreferences.Editor ecriture = donnees.edit();
     
    		ecriture.putString("nom", this.nom);
    		ecriture.putString("type", this.type);
    		ecriture.putLong("date", this.date.getTimeInMillis());
    		ecriture.putBoolean("active", this.active);
    		ecriture.putLong("intervale", this.intervale);
    		ecriture.putBoolean("inexact", this.inexact);
     
    		ecriture.apply();
    	}
     
    	public void recupereration(Context context)
    	{
    		SharedPreferences donnees = context.getSharedPreferences(this.nom, Activity.MODE_PRIVATE);
     
    		this.nom = donnees.getString("nom", "faux nom");
    		this.type = donnees.getString("type", this.type_defaut);
    		this.date.setTimeInMillis(donnees.getLong("date", 0));
    		this.active = donnees.getBoolean("active", this.active_defaut);
    		this.intervale = donnees.getLong("intervale", this.interval_defaut);
    		this.inexact = donnees.getBoolean("inexact", this.inexact_defaut);
    	}
    }

    et enfin la super classe:

    Code java : 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
    package com.bibi.chuut;
     
    import java.util.Calendar;
     
    import android.app.Activity;
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
     
    public class alarme {
     
    	protected String type_defaut = "normal";
    	protected boolean active_defaut = true;
     
    	String nom;
    	String type; //sert pour le setAction de l'intent
    	AlarmManager alarme_manager;
    	PendingIntent pending_intent;
    	Intent intent;
    	Calendar date;
    	boolean active;
     
     
    	public String getnom() {
            return nom;
        }
        public void setnom(String _nom) {
            this.nom = _nom;
        }
     
    	public String gettype() {
            return type;
        }
        public void settype(String _type) {
            this.type = _type;
        }
     
    	public Calendar getdate() {
            return date;
        }
        public void setcalendrier(Calendar _date) {
            this.date = _date;
        }
     
        public alarme(String _nom)
        {
        	this.nom = _nom;
        	this.type = type_defaut;
        	this.date = Calendar.getInstance();
        	this.active = false;
        }
     
    	public void activer(Context context)
    	{
    		this.active = true;
        	this.intent = new Intent(context,alarme_action.class);
    		this.intent.setAction(type);
    		this.pending_intent = PendingIntent.getBroadcast(context, 0, this.intent, 0);
    		this.alarme_manager.set(AlarmManager.RTC_WAKEUP, this.date.getTimeInMillis(), this.pending_intent);
     
    		enregistrement(context);
    	}
     
    	protected void enregistrement(Context context)
    	{
    		SharedPreferences donnees = context.getSharedPreferences(this.nom, Activity.MODE_PRIVATE);
    		SharedPreferences.Editor ecriture = donnees.edit();
     
    		ecriture.putString("nom", this.nom);
    		ecriture.putString("type", this.type);
    		ecriture.putLong("date", this.date.getTimeInMillis());
    		ecriture.putBoolean("active", this.active);
     
    		ecriture.apply();
    	}
     
    	public void recupereration(Context context)
    	{
    		SharedPreferences donnees = context.getSharedPreferences(this.nom, Activity.MODE_PRIVATE);
     
    		this.nom = donnees.getString("nom", "");
    		this.type = donnees.getString("type", this.type_defaut);
    		this.date.setTimeInMillis(donnees.getLong("date", 0));
    		this.active = donnees.getBoolean("active", this.active_defaut);
    	}
     
    	public void cancel(Context context)
    	{
    		this.active = false;
    		this.alarme_manager.cancel(this.pending_intent);
    		enregistrement(context);
    	}
     
    }



    je peux utiliser ces classes ? ou j'ai tout faux ?

    Merci.

  6. #6
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    Ok... compris....

    Bon, la notion de PendingIntent est un poil plus complexe à appréhender que les Intent. Les Intent sont simple: on y colle ce qu-on veut, et bim, on le lance au système.
    Un PendingIntent est un poil plus complexe: il doit résister à un arrêt/redémarrage du device, et surtout, transporte des "autorisations" avec lui.
    C'est grosso-modo un "token" d'utilisation de données applications... réutilisable à souhait par l'application destinataire, jusqu'à ce qu'un "cancel" soit réalisé dessus.

    Ce qui veut dire que l'on a pas un PendingIntent par Intent:

    Citation Envoyé par PendingIntent
    Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.
    Citation Envoyé par Intent.filterEquals
    Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.
    Donc si on fait deux PendingIntent à partir de deux Intent différents qui ont:
    * La même action (ce qui est le cas)
    * Les même données (URL, c'est le cas)
    * Le même data-type (mime-type, c'est le cas)
    * La même classe (le listener: c'est le cas)
    * La même catégorie (c'est le cas)

    Même si a chaque fois les "extra" sont différents, on aura en fait le même PendingIntent dans les deux cas... et donc on peut faire 20 appels à setRepeating, un seul sera pris en compte.

    Il y a une astuce pour différencier les PendingIntent si on ne peut vraiment rien faire autrement sur l'Intent, c'est utiliser le "requestCode" du getActivity ou getBroadcast.... Si le request-code est différent, le PendingIntent sera différent (même si les Intents sont identiques).

    Mais dans notre cas présent, passer l'identifieur d'alarme dans l'URL est encore le plus simple !

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

Discussions similaires

  1. Alarme répétitive ne se lance pas
    Par yrtera dans le forum Android
    Réponses: 8
    Dernier message: 24/03/2013, 18h53
  2. Déclenchement sur select
    Par sdinot dans le forum PostgreSQL
    Réponses: 6
    Dernier message: 02/04/2004, 11h52
  3. 1er déclenchement d'un trigger d'auto-incrément
    Par babylone7 dans le forum Administration
    Réponses: 11
    Dernier message: 11/03/2004, 16h21
  4. [langage] alarm( MILLISECONDES ?????)
    Par armada dans le forum Langage
    Réponses: 8
    Dernier message: 10/06/2003, 09h00
  5. Déclenchement Programme sur Virtual Key
    Par Tom-G dans le forum API, COM et SDKs
    Réponses: 9
    Dernier message: 09/05/2003, 12h58

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