我正在研究药物和约会的警报......当我设置警报时,我会在闹钟响起时添加额外的数据以便使用它们。 这里有一些代码在我的公共类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
…….
问题是我从intent额外数据中获得的所有数据总是返回null!
如果有人知道这个问题,我希望以最简单的方式回答我,因为我对Android很新... 等待帮助。
答案 0 :(得分:0)
您只在意图中设置了两个键:
但你试图获得未设置的密钥:
答案 1 :(得分:0)
检查您的拼写医学
设置时:
i.putExtra("MEDICINE", med.getName());
阅读时:
data.getCharSequence("MEDICNIE");
“ MEDICNIE ”与“医药”不一样