我正在使用以下代码启动警报,该警报应每天按设置的早餐时间显示通知
public void scheduleBreakfast(Context ctxt,int hours,int minutes,String state) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctxt);
if(prefs.contains("notify_breakfast")){
prefs.edit().putString("notify_breakfast",""+hours+":"+minutes+":"+state).commit();
}else
prefs.edit().putString("notify_breakfast","8:0:am").commit();
if(prefs.contains("notifyB")){
if(prefs.getBoolean("notifyB",true) == false)
return;
}else
prefs.edit().putBoolean("notifyB",true).commit();
Log.d("in breakfast","scheduling ....");
mgr = (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(ctxt, NotificationBreakfast.class);
PendingIntent pendingIntent = PendingIntent.getService(ctxt, 0, myIntent, 0);
long current_time = System.currentTimeMillis();
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY, hours);
time.set(Calendar.MINUTE, minutes);
time.set(Calendar.SECOND, 0);
long limit_time = time.getTimeInMillis();
if (current_time > limit_time) {
//nothing
} else {
//show notification
Log.d("breakfast","schedule");
mgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
和通知代码
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
if(prefs.getBoolean("notifyB", true)){
notifyShow();
}
}
问题是,如果时间设置为上午 8 点,它不会每天显示通知,也不会在早餐时间设置,它可能会在上午 8 点以及 8:03 和 8:05 触发.....