Search code examples
androidalarmmanagernotificationmanager

Android alarm with notification running any date when I change date


I am using AlarmManager and NotificationManager with BroadcastReceiver. When I set an alarm with a specific date, it is working the first time. However, when I modify the date and click the confirm button, the alarm is working any date immediately. I want to set the alarm interval day after expired date with fixed time. What is the problem with it? I don't understand at the moment.

confirmButton.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v) {
    //set alarm with expiration date                
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    setOneTimeAlarm();
    Toast.makeText(fridgeDetails.this, "Alarm automatic set", 
        Toast.LENGTH_SHORT).show();
    setResult(RESULT_OK);
    finish();
}

public void setOneTimeAlarm() {
    c.set(Calendar.HOUR_OF_DAY, 14);
    c.set(Calendar.MINUTE, 49);
    c.set(expiredYear, expiredMonth, expiredDay);
    Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
        fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, pendingIntent);
}
}); 

AlarmService.java

public class AlarmService extends BroadcastReceiver{
    NotificationManager nm;
    @Override
    public void onReceive(Context context, Intent intent) {
        nm = (NotificationManager) context.getSystemService(
            Context.NOTIFICATION_SERVICE);
        CharSequence from = "Check your fridge";
        CharSequence message = "It's time to eat!";
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(), 0);
        Notification notif = new Notification(R.drawable.ic_launcher,
            "Keep Fridge", System.currentTimeMillis());
        notif.setLatestEventInfo(context, from, message, contentIntent);
        notif.defaults |= Notification.DEFAULT_SOUND; 
        notif.flags |= Notification.FLAG_AUTO_CANCEL; 
        nm.notify(1, notif);
    }   
}

Solution

  • You need to set the property instead of FLAG_ONE_SHOT. This is for single alarm event not for the repeat. try this

    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    see more detail about from here

    Edit:

    As you do for notification with PendingIntent

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);
    

    In this you pass the empty object of Intent you need to pass the class name for that when you click on notification which Activity will be launch like in Alarm set time you do

    Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    now just pass the Acitivity name in the intent like suppose you want to launch your home activity and activity name like "homeactivity"

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,HomeActivity.class), 0);