Jump to content

Notification à une date et heure précise


AmineDrX

Recommended Posts

Salut, moi c'est Amine et je suis nouveau sur le forum donc j’espère ne pas m'être trompé de section.

J'ai un soucis depuis plusieurs jours en voulant afficher une notification à une date précise, en utilisant AlarmManager, BroadcastReceiver et Service. Je vous montre mon code :

 

Le Manifest :

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.todo.amine.mytodo"
    android:versionCode="2"
    android:versionName="1.1">
 
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
 
            </intent-filter>
        </activity>
        <service android:name=".MyAlarm"
            android:enabled="true" />
 
        <receiver android:name=".MyReceiver">
            </receiver>
        </application>
 
        </manifest>

createNotify() :

public void createNotify(int month, int year, int day, int hour, int m){
 
        Calendar c = Calendar.getInstance();
        Date temp = c.getTime();
        Calendar calendar = Calendar.getInstance();
 
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_MONTH, day);
 
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
       /** calendar.set(Calendar.AM_PM,Calendar.PM);**/
        Date pm = calendar.getTime();
 
        if(pm.after(temp))
        {
 
            Intent myIntent = new Intent(Main.this, MyReceiver.class);
         pendingIntent = PendingIntent.getBroadcast(Main.this, 0, myIntent,0);
 
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
 
 
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
        }
 
    }

MyAlarm.class

   
    public class MyAlarm extends Service
    {
 
        private NotificationManager mManager;
 
        @[member=override]
        public IBinder onBind(Intent arg0)
        {
            // TODO Auto-generated method stub
            return null;
        }
 
        @[member=override]
        public void onCreate()
        {
            // TODO Auto-generated method stub
            super.onCreate();
        }
 
        @SuppressWarnings("static-access")
        @[member=override]
        public void onStart(Intent intent, int startId)
        {
            super.onStart(intent, startId);
 
 
 
   mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),Main.class);
 
    Notification notification = new Notification(R.drawable.ic_appxhdpi,"This is a test message!", 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(), `"AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);`
 
    mManager.notify(0, notification);
        }
 
        @[member=override]
        public void onDestroy()
        {
            // TODO Auto-generated method stub
            super.onDestroy();
        }
 
    }

MyReceiver.class

  public class MyReceiver extends BroadcastReceiver
    {
 
        @[member=override]
        public void onReceive(Context context, Intent intent)
        {
            Intent service1 = new Intent(context, MyAlarm.class);
            context.startService(service1);
 
        }
    }

Les paramètres de createNotify sont bon, la méthode est bien appelée, le soucis c'est que le service n'est pas appelé ou bien il est appelé à n'importe quel moment.

J'ai essayé d'utiliser createNotify avec seulement deux paramètre : l'heur et la minute, une notification apparaît désormais mais pas à l'heure demandée. Elle apparaît dés que je fais appel à la méthode createNotify.

J'ai l'impression que le soucis se trouve dans l'objet calendar ?

Merci d'essayer de m'aider.

Link to comment
Share on other sites

  • 4 months later...

Pour info, depuis KitKat, l'appel à AlarmManager.set équivaut à AlarmManager.setInexact. C'est à dire que sur les version supérieures à KitKat, les alarmes paramétrées ainsi seront déclenchées au bon vouloir de l'OS (en fait en groupant les demandes des autres applications). Si tu tiens absolument à avoir une alarme exacte, il faut appeler setExact.

Je n'ai pas trop regardé le reste du code, il y a peut-être d'autres explications à ton dysfonctionnement.

Edited by Hervé Rilos
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...