0

Calendar calendars = Calendar.getInstance();
                        calendars.setTimeInMillis(System.currentTimeMillis());
                        calendars.set(Calendar.HOUR_OF_DAY, hour);
                        calendars.set(Calendar.MINUTE, min);
                        calendars.set(Calendar.SECOND, 0);
                        calendars.set(Calendar.MILLISECOND, 0);
                        calendars.set(Calendar.AM_PM, timezone);
                        
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), currentTime, intent, PendingIntent.FLAG_UPDATE_CURRENT);


if (Build.VERSION.SDK_INT < 23) {
                        if (Build.VERSION.SDK_INT >= 19) {
                            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendars.getTimeInMillis(), pendingIntent);
                        } else {
                            alarmManager.set(AlarmManager.RTC_WAKEUP, calendars.getTimeInMillis(), pendingIntent);
                        }
                    } else {
                        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendars.getTimeInMillis(), pendingIntent);
                    }

In my App if I set Alarm within an hour then it is working fine but for long duration it is not working.

I have searched a lot regarding the alarm issue we are facing and found out that in latest android updates android OS is killing the broadcast receivers if memory needed for other task OR in low battery state OR without a reason.

In some cases if mobile is in idle / doze mode that time also alarm is not working.

You can check below links.

1) https://www.fusonic.net/en/blog/why-my-broadcastreceiver-does-not-get-called/

2) sendWakefulWork not always called with cwac-wakeful-1.1.0

3) Alarm in Android stops when some hours has passed

4) https://stackoverflow.com/a/42468819

I have found that if you killed the app from background then all alarm receiver getting cleared. For this you have to set autostart enabled for your app.

However we can not set it pro-grammatically because all mobile brand have different methods to set this.

a. VIVO mobile - settings - more setting - Application - autostart - Enable Autostart for your app.

b. Redmi mobile - security app > permissions > auto-start - Enable Autostart for your app.

And in code I have made Background Services which will running in background and reseting the alarms in every predefined intervals.

After doing this still morning alarms are not working on some devices like Redmi note 4, samsung s3

In other devices alternate alarms were fired. example- if i set 3 alarm on 7,8,9 am. only 2-3 were working.

Is Android OS killing BG services too? Please help.

  • 1
    Are you sure your hour and min are in future? You dont have to call calendars.setTimeInMillis(System.currentTimeMillis()); since Calendar.getInstance() already points to current data & time – Sagar May 15 '18 at 08:00
  • yes user can select the time from clock mode timepicker UI and i m passing it in hour and min variables. – Nikita Kurliye May 15 '18 at 08:58
  • But by setting setcurrentmiilis again is not creating the problem as my alarms are working if set within an hour. please suggest for long hours alarms. Receiver is not hitting as OS kills it randomly. – Nikita Kurliye May 15 '18 at 09:22

0 Answers0