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

流过昼夜 提交于 2019-12-12 02:57:39

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/35832166/alarmmanager-doesnt-work-on-samsung-device-with-android-lollipop-5-0-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!