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

Android AlarmManager problem with setting & resetting an alarm

When canceling the AlarmManager do not use a PendingIntent with a flag of FLAG_CANCEL_CURRENT. Instead, cancel the PendingIntent explicitly after canceling the alarm: am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE); Intent i = new Intent(getApplicationContext(),AlarmReceiver.class); PendingIntent p = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0); am.cancel(p); p.cancel();

AlarmManager setExact with WakefulBroadcastReceiver sometimes not exact

For Marshmallow era(?), we need some ugly codes like below… 🙁 And “delayInMillis” param should be more than 15 minutes on the API 23. If not, system ignore the minutes less than 15 minutes. private void registerExactAlarm(PendingIntent sender, long delayInMillis) { final int SDK_INT = Build.VERSION.SDK_INT; AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); long timeInMillis = (System.currentTimeMillis() … Read more

Android Alarm Manager with broadcast receiver registered in code rather than manifest

How about this? Intent startIntent = new Intent(“WhatEverYouWant”); PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0); AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, startPIntent); And then in your Manifest.xml file: <receiver android:name=”com.package.YourOnReceiver”> <intent-filter> <action android:name=”WhatEverYouWant” /> </intent-filter> </receiver> So as far as I know you still have to declare the receiver in the Manifest. I’m not … Read more

SharedPreferences in BroadcastReceiver seems to not update?

I had the same problem and after struggling for hours to solve it, I finally found the issue causing it. In your AndroidManifest you probably have something like that: <receiver android:name=”AlarmReceiver” android:process=”:remote” /> The last attribute (process:remote) cause the receiver to run on a different/new process when it is called. But SharedPreferences is NOT supported … Read more

Android Oreo killing background services and clears pending alarms, scheduled jobs after entering doze mode

You would not be able to run background services long running in Oreo as there are behaviour changes, now Oreo to optimise system memory, battery etc, it kills background service, to solve your issue you should use foreground service. Have a look at Background execution limits https://developer.android.com/about/versions/oreo/android-8.0-changes A suggestion from me, if you can use … Read more