2

I'm working on alarms for medicines and appointments … when i set an alarm i put extra data to use them later when the alarm go off…. here is some code to set a medicine alarm in my public class AlarmUtil :

 private static void setLimitedDurationAlarms(Context ctxt, MedicineClass med)
{
    long ONE_DAY = 86400000;
    AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
      // set up the first alarm
    Calendar firstDoseTime = med.getFirstDoseTime();
    // get firstDoseDate
    Calendar firstDoseToday = med.getStartDate();
    // set the time for the first dose for today.
    firstDoseToday.set(Calendar.HOUR_OF_DAY, firstDoseTime.get(Calendar.HOUR_OF_DAY));
    firstDoseToday.set(Calendar.MINUTE, firstDoseTime.get(Calendar.MINUTE));
    Intent i = new Intent(ctxt, OnAlarmReceiver.class);

    i.putExtra("MEDICINE", med.getName());
    i.putExtra("LAST_ALARM", "FALSE");

    PendingIntent pi = PendingIntent.getBroadcast(ctxt, getUniqueID(), i, 0);
    mgr.set(AlarmManager.RTC_WAKEUP, firstDoseToday.getTimeInMillis(), pi);
      ……….
     ……

When receiving the alarm…. i need to get the extra data of the alarm to know if it is for medicine or appointment.. and also to use the specific data of each med or app to get the object and show its info with the notification .. as shown in the next code ..

public class OnAlarmReceiver extends BroadcastReceiver
{


@Override
public void onReceive(Context ctxt, Intent intent)
{
    Log.d("Alarm", "Alarm OFF! BEEP BEEP BEEP!");

    Bundle data = intent.getExtras();
    String medicine = (String) data.getCharSequence("MEDICNIE");
    String appointment = (String) data.getCharSequence("APPOINTMENT");
    String AppAction = (String) data.getCharSequence("APP_ACTION");

    if (medicine == null)
       // this alarm is not for medicine = for App
    // use "appointment" values to get the appointment object from appointment list
      else
       // this is medicine alarm..
       // use "medicine" value to get the medicine object form medicines list
        …….

The problem is that all the data i get from the intent extra data always return null !

Please if any one know about this problem, i hope to answer me by the simplest way because i'm very new to android ..
waiting for a help .

Ysak
  • 33
  • 1
  • 5

2 Answers2

0

You only set two keys in the intent:

  • MEDICINE
  • LAST_ALARM

But you try to get the unset keys:

  • APPOINTMENT
  • APP_ACTION
MByD
  • 135,866
  • 28
  • 264
  • 277
0

Check your spelling for Medicine

When setting:

i.putExtra("MEDICINE", med.getName());

When reading:

data.getCharSequence("MEDICNIE");

where as "MEDICNIE" is not same as "MEDICINE"

waqaslam
  • 67,549
  • 16
  • 165
  • 178