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

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