为什么获取额外的数据总是返回null?

问题描述:

我正在处理药物和约会的警报......当我设置闹钟时,我会在闹钟响起后放置额外的数据以便使用它们。 这里是一些代码来设置一个报警药在我的公共类AlarmUtil:为什么获取额外的数据总是返回null?

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); 
     ………. 
    …… 

当接到报警...。我需要获取警报的额外数据,以了解它是否用于医疗或预约..还可以使用每种医疗或应用程序的具体数据来获取对象并通过通知显示其信息。下一个代码..

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 
     ……. 

问题是我从意图额外数据中获得的所有数据总是返回null!

请如果有人知道这个问题,我希望以最简单的方式回答我,因为我很新的android ..
等待帮助。

+0

其中ar您是否在捆绑中设置值? – waqaslam 2012-04-20 11:55:16

你只设置在意向两个键:

  • 医药
  • LAST_ALARM

但你尝试获得未设置键:

  • 委任
  • APP_ACTION
+0

非常感谢您的第一次重播..是的,他们用于其余的代码:) – Ysak 2012-04-20 12:31:38

检查拼写医学

当设置:

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

读取时:

data.getCharSequence("MEDICNIE"); 

这里为 “MEDICNIE” 并不等同于 “医药

+0

O_o omg .. thaaaaanks! – Ysak 2012-04-20 12:30:25