alarmmanager
Need code example on how to run an Android service forever in the background even when device sleeping, like Whatsapp?
NOTE: NOW THIS ANSWER IS ONLY VALID FOR ANDROID 7 AND BELOW. SINCE ANDROID 8 GOOGLE HAS CHANGED HOW BACKGROUND TASKS ARE HANDLED Since I posted this question, I have implemented two different approaches to this solution into multiple apps. APPROACH 1 This extract is from an app where I use push notifications, which need … Read more
java.lang.SecurityException: !@Too many alarms (500) registered from pid 10790 uid 10206
Unlike what the comment suggests, this might not be your fault. This started happening for us in production code somewhere mid March, this only happens on Samsung with Lollipop which only recently started to be rolled out. Update: This issue finally happened on one of the phones we have available. like @goncalossilva said the problem … Read more
How to set an alarm to be scheduled at an exact time after all the newest restrictions on Android?
Found a weird workaround (sample here) that seems to work for all versions, including even Android R: Have the permission SAW permission declared in the manifest: <uses-permission android:name=”android.permission.SYSTEM_ALERT_WINDOW” /> On Android R you will have to also have it granted. On before, doesn’t seem like it’s needed to be granted, just declared. Not sure why … Read more
Job Scheduler vs Background Service
I have an app which has a feature A which should run in background every minute. That will not happen on hundreds of millions of Android devices, those running Android 6.0 and higher, due to Doze mode (and, possibly, app standby, depending on the rest of your app). But AlarmManager seems to be a good … Read more
WorkManager vs AlarmManager, what to use depending on the case
Should I use AlarmManager directly for this ? Yes you should. AlarmManager is the best option as far as I know to handle tasks like yours and also is the safer option when dealing with doze mode. Use the first alarm to set the second alarm at a specific time. Should I use WorkManager to … Read more
Should I use PendingIntent.getService() or getBroadcast with AlarmManager?
One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started. it has one more step in starting service than Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off. then you should use … Read more