Android 为什么获取额外数据总是返回null?
我正在为药物和约会制作闹钟…当我设置闹钟时,我会在闹钟响起时放入额外的数据以供日后使用…。 下面是一些在我的公共类AlarmUtil中设置药物警报的代码:Android 为什么获取额外数据总是返回null?,android,alarms,Android,Alarms,我正在为药物和约会制作闹钟…当我设置闹钟时,我会在闹钟响起时放入额外的数据以供日后使用…。 下面是一些在我的公共类AlarmUtil中设置药物警报的代码: private static void setLimitedDurationAlarms(Context ctxt, MedicineClass med) { long ONE_DAY = 86400000; AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Co
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);
……….
……
收到警报时…。我需要获取闹钟的额外数据,以知道它是用于医疗还是预约。。还可以使用每个med或应用程序的特定数据获取对象,并在通知中显示其信息。。如下面的代码所示
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
…….
问题是,我从intent额外数据中获得的所有数据总是返回null
如果有人知道这个问题,我希望用最简单的方式回答我,因为我对android非常陌生。正在等待帮助。您仅在意图中设置了两个键:
- 药
- 最后警报
- 任命
- 应用程序行动
- 药
- 最后警报
- 任命
- 应用程序行动
i.putExtra("MEDICINE", med.getName());
阅读时:
data.getCharSequence("MEDICNIE");
其中,“MEDICNIE”与“MEDICINE”不同。请检查您对MEDICINE的拼写 设置时:
i.putExtra("MEDICINE", med.getName());
阅读时:
data.getCharSequence("MEDICNIE");
其中“MEDICNIE”与“MEDICINE”不同。您在捆绑包中的哪些位置设置值?您在捆绑包中的哪些位置设置值?非常感谢您的首次重播。。是的,它们用于代码的其余部分:)非常感谢您的第一次重播。。是的,它们用于代码的其余部分:)