我正在为药物和约会设置警报……当我设置警报时,我会添加额外的数据,以便稍后在警报响起时使用它们……。这是在我的公共类 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);
……….
……
接到警报时…… 我需要获取警报的额外数据以了解它是用于药物还是约会.. 并且还需要使用每个药物或应用程序的特定数据来获取对象并在通知中显示其信息.. 如下一个代码..
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 很陌生 ..
等待帮助。