0

I am running this code to set a Alarm Manager in my MainActivity on create method

public void notificationCheck() {

    calendar.set(Calendar.HOUR_OF_DAY, Preferences.getMorningHour(getApplicationContext()));
    calendar.set(Calendar.MINUTE, Preferences.getMorningMinute(getApplicationContext()));
    calendar.set(Calendar.SECOND, 0);

    final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), 0);

    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        am.setAndAllowWhileIdle(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    } else {
        am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

So this should set the alarm daily at 10:00 AM, The alarm is working fine until it's fired once. Once the alarm is done at 10:00 AM it keeps going off each time i open the app.

Can some please explain if i need to make any code changes?

EDIT :

I am using sharedPreferences to set the time of the Calendar Instance

Preferences:

public static int getMorningHour(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context).getInt(MORNING_HOUR, 9);
}

public static void setMorningHour(Context context, Integer morningHour) {
    PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(MORNING_HOUR, morningHour).apply();
}

public static int getMorningMinute(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context).getInt(MORNING_MINUTE, 0);
}

public static void setMorningMinute(Context context, Integer morningMinute) {
    PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(MORNING_MINUTE, morningMinute).apply();
}

... and I set the Preferences in my app Settings using TimePicker Dialog

@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {

    Preferences.setMorningHour(getApplicationContext(), hourOfDay);
    Preferences.setMorningMinute(getApplicationContext(), minute);

}
Sasi Kanth
  • 709
  • 8
  • 17
  • See the below links. its work for me. [Repeat Alarm](http://stackoverflow.com/a/40311030/3782085) – Magesh Pandian Oct 29 '16 at 04:58
  • The code is same, i am repeat a alarm daily. I am asking for a fix for the AlarmManager going off at start of the Activity – Sasi Kanth Oct 29 '16 at 05:05
  • are you tried to use broadcast receiver with this ? – Radhey Oct 29 '16 at 06:54
  • Yes i am using broadcast receiver. – Sasi Kanth Oct 29 '16 at 06:58
  • Can you show the code of your MainActivity? – Marat Oct 29 '16 at 07:58
  • The MainActivity code has various other methods don't effect this. But i have to found a way to get what i wanted. I have checked for current time in milliseconds with the alarm time and if alarm time is before the current time i am adding a day to the calendar so that way i am not getting a duplicate notifications. – Sasi Kanth Oct 29 '16 at 16:00

1 Answers1

0

when start Activity , you need to check alarm already live or not. if live dnt disturb that. see below code.

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);

boolean isWorking = (PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_NO_CREATE) != null);
if (isWorking) {Log.d("alarm", "is working");} else {Log.d("alarm", "is not working");}

if(!isWorking) {
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,    PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    int timeNotif = 5 * 60 * 1000;//time in ms, 7*24*60*60*1000 for 1 week
    Log.d("Notif", "Notification every (ms): " + timeNotif);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), timeNotif, pendingIntent);
    }
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60
  • It's not working, evertime i close and reopen the MainActivity alarm manager goes off. Basically what my issue with it is, the Alarm Manager is going of for the past value of the time. So, like if i set a Alarm for 10:50 everything works fine the alarm manager wont go off when i open the app and functions as usual at 10:50. But once that time is done the app just fires the alarm manager everytime i open the app – Sasi Kanth Oct 29 '16 at 05:21