AlarmManager重复功能无法按预期工作

时间:2013-10-03 06:43:43

标签: android repeatingalarm

我正在使用以下代码启动闹钟,该闹钟应每天按时设置通知

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分点火.....

0 个答案:

没有答案