Search code examples
androidandroid-intentbroadcastreceivertaskalarm

Android Alarm is not working after updating time


I have set repeating alarms, it works well but when I update the time the alarm does not work. I'm using same code and flag in pending intent but creating and updating from different activity. In log it says the alarm is triggered at specific time but it is not working. can anyone say what is the problem?

Calling alarm from Insert activity:

  startAlarm(Insert.this, pillName, timeInMilis, code);

Calling alarm from Edit activity:

startAlarm(Edit.this, pillName, timeInMilis, code);

Function for creating and updating alarm:

public void startAlarm(Context context, String pillName, long time, int code) {
    Intent aIntent = new Intent(context, AlarmReceiver.class);
    aIntent.putExtra("pillName", pillName);
    aIntent.putExtra("code", code);
    PendingIntent pIntent = PendingIntent.getBroadcast(context, code, aIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time,60000, pIntent);
}

Broadcast Receiver class:

public void onReceive(Context context, Intent intent) {
    String pillName=intent.getExtras().getString("pillName");
    MediaPlayer mediaPlayer=MediaPlayer.create(context, Settings.System.DEFAULT_NOTIFICATION_URI);
    mediaPlayer.start();
    Toast.makeText(context,"Testing Alarm::"+pillName,Toast.LENGTH_SHORT).show();

}

Solution

  • When you start before stop the AlarmManager pending intent. It's may be working well