2

I am using the following code to start alarm which should shows notification daily on time set for breakfast

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);
    }   

}

and the code for notification

@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();  

        }
    }

the problem is , it doesn't show notification daily and not in time set for breakfast if time set is 8 am , it may fire at 8 am and also on 8:03 and 8:05 .....

M.ElSaka
  • 1,264
  • 13
  • 20
Sarah Sami
  • 577
  • 1
  • 6
  • 20

0 Answers0