3

我正在尝试使用此代码弹出警报消息。它在启动或打开应用程序时工作,但在应用程序之外没有任何弹出消息。我很困惑,我不知道我做错了什么。

String alarmtime = cur.getString(cur.getColumnIndex(DBDATA.LG_ALARMTIME));
//Reminder
String[] timesplit = alarmtime.split(":"); 
int hour = Integer.parseInt(timesplit[0]);
int minute = Integer.parseInt(timesplit[1]);
System.out.println(hour);
System.out.println(minute); 

AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ShortTimeEntryReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 

Calendar alarm = new GregorianCalendar();
alarm.setTimeInMillis(System.currentTimeMillis());
alarm.set(Calendar.HOUR_OF_DAY, hour);
alarm.set(Calendar.MINUTE, minute); 
alarm.set(Calendar.SECOND, 0);  
System.out.println(System.currentTimeMillis());
System.out.println(alarm.getTimeInMillis());
if (System.currentTimeMillis() > alarm.getTimeInMillis()){
    alarm.setTimeInMillis(alarm.getTimeInMillis()+ 24*60*60*1000);// Okay, then tomorrow ...
    alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);
}
else
{
    alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);                 
}

我需要在应用程序(即)之外弹出警报消息,就像警报一样。谢谢你们的帮助,

4

1 回答 1

2

您可能需要一个BroadcastReceiver

正如您在这个问题中所读到的那样:BroadcastReceiver not received an alarm's broadcast

您必须像这样构建意图:

Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, "CHECK_ALARM_CODE", alarmIntent, 0);

并收到这样的警报:

    public class AlarmReceiver extends BroadcastReceiver{
      @Override
      public void onReceive(Context context, Intent intent) {
         Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
         Log.d("OK", "AlarmReceiver.onReceive");
      }
}

不要忘记在清单文件中注册您的广播。

于 2012-05-22T12:40:42.170 回答