Search code examples
androidalarmmanagerandroid-5.0-lollipopsamsung-mobile

AlarmManager doesn't work on samsung device with Android lollipop 5.0.2


I have an issue with the AlarmManager API, it doesn't fire at all on my samsung device, but it works on other Android device...

My code is like that:

 public void setAlarm(int hourAlarm,  int minutesAlarm, int numeroAlarm, boolean yesOrNoRepeatAlarm) {


    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, numeroAlarm, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Log.i("numeroAlarm", numeroAlarm + "");
    Calendar calendar = Calendar.getInstance();

    if( (calendar.get(Calendar.HOUR_OF_DAY) > hourAlarm) ||
            ( calendar.get(Calendar.HOUR_OF_DAY) == hourAlarm && calendar.get(Calendar.MINUTE) > minutesAlarm)) {

        calendar.add(Calendar.DATE, 1);
    }

    calendar.set(Calendar.HOUR_OF_DAY, hourAlarm);
    calendar.set(Calendar.MINUTE, minutesAlarm);

    Log.i("timeInMillis", calendar.getTimeInMillis() + "");
    Log.i("repeatingAlarm", yesOrNoRepeatAlarm + "");

    if(yesOrNoRepeatAlarm) {

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
    else {

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

}

It works perfectly on other device (specially on kitkat) but not on lolipop in my samsung device, do you have any idea please ?


Solution

  • I had the same problem and after a lot of searching and workaround, I figured that Smart Manager of Samsung Devices in Lollipop and above may be the problem. This component can delay the Alarm Manager to goes off, it is triggered after 3 minutes, if the mobile is working on battery and with the screen closed. Of course you can deactivate the Smart Manager look at this :

    • Launch Samsung Smart Manager application on the device
    • Tap Battery
    • Tap App optimization
    • Detail
    • Find Your APP
    • Select "Disabled for"

    But in my case it didn't work, neither disabling the Smart Manager worked in 2 Samsung devices with Lollipop . What it did work was to "fool" Smart Manager by refactoring the name of my application's package to contains the String "alert" or "alarm", for example com.example.alarm.myApplication. You can also refer to this link for more information.