07
Mar
3261001723_77e1f4acbc_b

One of the fundamental differences between Android and every other mobile operating system is the practically unrestricted capability to run services. Without this freedom we could not enjoy something as powerful as a homescreen widget or as straight-forward as a Twitter client with background updates. Aside from games and very simple utilities, it’s becoming increasingly rare to find an app that doesn’t run a service, at least for a short span of time. However, a bug has snuck into recent versions of Android and it can cripple background processing in some apps and widgets.

1148-38795-s6o6DG7F4zEWJyD-upload

Note: There are some technical details ahead, some of which may only matter to developers. Even if you don’t write code, don’t turn away. Everything should still be very easy to read and comprehend.

What’s Broken?

A great advantage to running a service is that an app’s interface can be closed but the process can continue running. This allows apps and widgets to rely on services to be operational without the user having to start up an app manually. Even in circumstances where a service has to be shut down -like freeing up memory for a game- the system can restart it later. The same rules apply to closing an app from the Recents screen (aka. task switcher or multi-tasking screen). This is all supposed to work automatically, so long as stopWithTask is set to false, which happens when the service returns START_STICKY or START_REDELIVER_INTENT from onStartCommand.

The troubles occur when a service is shut down by the system, either as a result of low memory or a user stopping it through the Recents screen. The system is supposed to automatically restart the service, but this bug prevents that from happening. Furthermore, BroadcastReceivers tend to become unresponsive when this bug comes into effect. This behavior can be a real problem to apps and widgets that expect to query for data from the Internet or update the content displayed on their widgets. Unfortunately, there really aren't any other obvious signs that something has gone wrong.

I’ve written a sample app to demonstrate the issue (and a workaround). As long as the service is running, it will produce a toast message every few seconds. If you would like to try it for yourself, the apk can be downloaded here, and the full code is available on GitHub. Take a look at the app and bug in action.


What Is Affected And How

The bug was introduced in Android 4.4.1 during a series of small patches meant to fix some other issues related to the service lifecycle. The side-effects did not become evident until after 4.4.2 had begun rolling out shortly after. Stock Android is definitely affected, meaning most Nexus devices will present with the issue, but it looks like some OEMs have independently fixed the issue before shipping their KitKat updates.

Strictly speaking, this bug only affects permanent or semi-permanent services - those that are supposed to continue running after an app’s interface has been closed. No reason has been clearly established, but Google’s own apps seem to be immune to the issue.

The two most common reasons for a service to be killed are related to low memory or actions by the user. Since most modern smartphones and tablets are shipping with 2 GB of RAM, it takes quite a powerful game or a lot of background services before the system will step in to remove something. Still, if you ever come out of a game and notice your widgets have become stale, this is probably the reason.

The far more common catalyst for this bug is the Recents screen, which allows users to dismiss apps. This action typically kills and restarts any services attached to the app, acting sort of like a reset switch for apps when they aren’t behaving properly. Since this can only be done with apps that have a UI in the Recents screen, services without an app are generally safe.

Workarounds

There are really aren’t many things regular users can do to reduce the effect of this bug. To reduce the likelihood of it occuring, don’t swipe away apps from the Recents screen any more than necessary. I hate that prospect as much as anybody, but it helps. If a service is killed, simply starting up the app that owns it should be enough to get it started back up. In the event that low memory is responsible for killing a lot of services, it’s probably just easier to restart the device and allow everything to come back on its own.

Developers do have an option for working around this issue in their own apps, but it’s not exactly ideal for all cases. While BroadcastReceivers and other similar mechanisms seem to be crippled, the AlarmManger can be used to refresh dead services. An alarm can be used to resurrect dead services. Of course, this approach isn’t without its drawbacks. Not only can adding alarms or using them for different purposes add complexity to an app, it can lead to higher battery consumption if the the device is repeatedly woken up from sleep.

As I mentioned earlier, my sample app implements a workaround using AlarmManager. Every app has different requirements, so this should not be considered a complete solution, rather a demonstration that the workaround is viable. If you should decide to implement something like this in your own app, be careful to set the alarm for a much more reasonable delay.

2014-03-07_08-28-43

Current Status: A Fix Is On The Way

Developer Liangcai Li of nVidia submitted a patch in mid-Feburary which has been officially merged into AOSP. It’s likely we’ll see this fix included with the next maintenance release or version update, but there is obviously no formal timeline for when that will roll out. There has yet to be a formal response in any of the AOSP Issue tracker threads. We still might have to wait a while for this fix to make its way out to Nexus devices (and possibly some OEMs), but it’s good to know that it won’t be left hanging for long.

Sources: AOSP Issue Tracker 1, 2, 3, 4, StackOverflow

Thumbnail Image Credit: Michael Kappel

Cody Toombs
Cody is a Software Engineer and Writer with a mildly overwhelming obsession with smartphones and the mobile world. If he’s been pulled away from the computer for any length of time, you might find him talking about cocktails and movies, sometimes resulting in the consumption of both.

  • kaid

    Eagerly hoping to see 4.4.3 released very very soon

    • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

      I kid you not, I've been determined to get this post done by the end of this week because I was a little afraid 4.4.3 might roll out before I could get it out. :)

      • Testraindrop

        Seems like you know more than you are allowed to say...

        Need date now! :D

        • https://play.google.com/store/apps/developer?id=iWizard Bikram Agarwal

          Yea, cough it up Cody.

        • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

          Believe me, I wish. I've just seen quite a few things stacked up that warrant one more maintenance release before the long wait until 4.5/5.0, so I thought the odds were good that something was coming soon. After all, it has been about 3 months since 4.4.2 landed, a release in the very near future would be quite timely.

          • Crispin Swickard

            Maybe the article will help poke the Google beehive into action. :) This has been a huge annoyance the last couple months. Thanks for the explanation of the irritation. I didn't, and then did intend that to rhyme.

  • Bambalazs

    ding

    • Mayoo

      ding

      • Bambalazs

        Service Started

    • Guest

      Ding

  • https://play.google.com/store/apps/developer?id=iWizard Bikram Agarwal

    So, That's what's happening with DashClock widget? It fails to update the numbers on the widget pretty often and I've been seeing long alarm wakes in BBS logs.

    • vyktorsouza

      same thing happens to the SoloWeather widget in my Nexus 5

  • blahmoomoo

    Hmm, I wonder if that would explain why on occasion Battery Widget Reborn occasionally stops updating until I launch the app manually. When this happens, the notification stops updating and the DashClock extension reads the battery as 0.

    I've also occasionally encountered an annoyance where Play Music simply dies within minutes of connecting to my car radio and pushing the car's play button doesn't start it back up (most times it just pauses within a few minutes for no reason and then works fine after pushing play), but I don't know if that's related.

    • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

      I can almost promise you that this bug isn't related to your issue with Play Music. Does Play Music die, or is it just the bluetooth connection that drops out?

      • blahmoomoo

        Play Music dies. The Bluetooth connection remains since the radio still indicates it's connected. I just have to unlock my phone and press play to resume. Sometimes it continues to die though, and then I actually have to open the app instead of using the homescreen widget.

        And when I say occasionally for Play Music, I mean every few weeks.

        Nexus 4 here.

        Could be ART related, maybe.

        • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

          Very strange, I haven't heard of that exact behavior. Have you tried uninstalling it and reinstalling. What phone are you using (and have you modded it)?

          • blahmoomoo

            Nexus 4. Only mod is using ART instead of Dalvik; no rooting or anything like that. Play Music is a system app, so I can't uninstall it, though it looks like I can uninstall updates.

            Honestly I'm not too bothered by it since it doesn't happen too often, but it is a bit annoying. Usually if I remember to start playing music before walking to my car, it's ok.

          • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

            Sorry, yeah, I meant uninstall updates. Clearing data should work, too, but I always feel like reinstalling an app might shake loose some other issue.

          • blahmoomoo

            I guess I could try that sometime. Since it's all on the cloud, I guess clearing data shouldn't affect anything except the songs I've pinned.

    • Simon Belmont

      I use Battery Widget Reborn, too. I haven't had an issue where it stops updating unless I manually kill the process (something I'd never do, BTW).

      By the way, I have a Nexus 5. I guess it could be related, but it's not affecting me.

  • Whyzor

    Good analysis. Seems to expose the lack of quality control higher up in Android, if OEMs & other 3rd parties are fixing these for Google.

    • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

      I'm absolutely certain HTC has fixed it, and I've heard some hints that Samsung fixed it for the Note 3 (less certain about the GS4). I've got no idea if Sony, LG, or any of the others are on top of this one yet.

      • Andro Tuning

        I have a Note 3 and this was not fixed last time I checked. Maybe there's a firmware update now.

    • duse

      Yep, Google has a real QA problem. I understand they like rapid iterative development and the "every engineer is a tester" philosophy, but clearly it doesn't work as well as it should. Something like this is BASIC and part of the core platform, how does it get through on a point update? They need more thorough automated testing and probably more staff dedicated only to testing and nothing else.

      • Andro Tuning

        Maybe because they only use Google apps which are not affected by these issues, so they didn't see this coming. G-apps are not affected because they all use synchronization mechanisms instead of background services, so they run all the time draining every-ones battery while these new bugs are deteriorating user experience with all other apps!

  • Simon Belmont

    Really hoping to see Android 4.4.3 pushed shortly. There's a bunch of niggling issues that really do deserve a MR bump before we see the next major update.

    Sadly, I don't think this one is going to be addressed: http://code.google.com/p/android/issues/detail?id=62805 . It's been affecting me since I got my Nexus 5 in November, and now it affects my Nexus 7 (2012) as well (after the KitKat update). Only 35 stars so far, though, even though you see more and more folks are being affected by (Moto X, Moto G, etc) after the KitKat update.

    • Andro Tuning

      Same, longing for 4.4.3, unfortunately the published fixes are not committed because some pending conflict and nothing's less certain whether they actually fix this particular issue.

      • Simon Belmont

        The email issue where I can't download my attachments is driving me nuts. The funny thing is, it seems to work (for the most part) on CM11 using the same KitKat email client, so something must have been fixed by a CM developer.

        As for everything else, I really hope we see Android 4.4.3 before the end of March or early April at the latest. I hope the fixes are committed and things can get rolling soon.

  • Cesar

    My Verizon Moto X doesn't seem to be affected. I swipe the app away and the service immediately starts back up.

    ding.

    • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

      Good test, thanks!

      • Cesar

        Looks like I spoke too soon. It worked the first few times, and then suddenly the bug kicked in. Can't get it to work again.

    • Guest

      Spoke too soon. It worked the first few times, and then suddenly the bug kicked in.

  • duse

    While on this topic, I still don't understand why Google doesn't include a simple "Allow background services" toggle on apps' info screens, like it does with "Show notifications." I understand disabling services could break apps, but it's not something that would ever be touched unless a user chose to, and it could have a big "Are you sure? This could break the app" popup when you check it. I love Android's flexibility when it comes to multitasking, but it's truly ridiculous that users have to deal with some of the battery-hogging crap apps pull or resort to some root solution to prevent it. It makes you have to carefully vet every app you install to make sure it's not going to misbehave.

    • Grayson

      Yup. I tend to just uninstall apps that run a service I don't want in the background, but some apps I really want to use so I just put up with the constantly running service.

    • Mike Reid

      Best idea I've heard. Google's "solution" was to force background services to show in notification shade, so the user knows at least.

      >but it looks like some OEMs have independently fixed the issue before shipping their KitKat updates.

      Nexus AOSP is great, but users need to consider that it acts like a "beta" program to some extent. There are REAL, VALID reasons why OEMs take longer to put out their updates than Google does.

      • Andrew

        The notification shade enforcement is only with apps running with a flag to mark them are running in the foreground. Most apps don't need to do this as their background service isn't usually that important to be running 100% of the time if it restarts correctly.

      • Cerberus_tm

        Yes, and that is really annoying: I don't want a Tasker icon clogging up
        my notification tray all the time. Notifications should only be for
        things that mark a recent change in something, like new messages.

    • Cerberus_tm

      They should integrate something like Greenify. It works perfectly, is flexible, and it can actually save you RAM without any cost in functionality by shutting down services but triggering them upon a push notification. This does not work for applications, but it does work for a great many, including the Play Store and Whatsapp (yeah, we should all switch to Telegram, and I'm trying to, but I still also need to run Whatsapp).

      • Andro Tuning

        I much prefer Android Tuner for preventing background services, works like duse mentioned, can be applied to 100 user/system apps alike and has far less impact on battery than Greenify. Greenify on the other side runs in the background and cannot handle too many apps without impacting overall performance.

        • Cerberus_tm

          Some applications break if you disable services or receivers; yes, I do that some some applications, while Greenify works for others. If anything, Greenify improves my battery life btw.

  • Barnassey

    I wonder if this explains why my nexus 5 drops calls on lte, as well as not receiving text message or phones at times.

  • John Smith

    "Since most modern smartphones and tablets are shipping with 2 GB of RAM, it takes quite a powerful game or a lot of background services before the system will step in to remove something."
    -> since the heap size could be 64MB (varies by device), it does indeed require a lot of background services

    However, because 4.4 cripples the SDcard, I'm sticking to 4.3 on my SGS4. (I wish I could remove the "System update downloaded" notification so that I don't accidentally press it.

    • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

      RE: update notification - Since I keep doing different tests on different versions, my Nexus 4 spends 90% of its time on 4.3. I would love to make that notification disappear (without hacking).

    • Harsha

      Can't you simply press and hold on that notification and then app info and then tick on disable notification or simply disable that app in app info screen?

      • John Smith

        It's the "Google Services Framework" that's the responsible app. I did turn off notifications (remove checkmark) and the notification is now gone :-)

        Hopefully there's no side-effects (i.e. me forgetting that I did this) and then wondering why I'm not getting firmware updates.

        • Cerberus_tm

          You could install Autoruns or a similar application to temporarily disable the Google update service. I have done that. If you Google it, it's easy to do.

      • Andrew

        It depends if you actually want notifications from that app. The notification not only suppresses the visual notification but also sounds, vibration and leds if raised through the notification API.

    • Chandler Scarborough

      I agree completely!

  • PhineasJW

    We've run into this issue on our app as well. Glad to hear a real fix is finally on the way. Thanks Cody.

  • Tim Meader

    Just a note: if you look at the change notes on android-review it actually hasn't been merged yet into Google's source tree. So, yeah, while it might be in AOSP, I'm not sure that will help the pending 4.4.whatever just yet unfortunately :(

  • saucy

    ding

  • Dave

    That explains Facebook messager aswell .

    • Crispin Swickard

      I thought the app was broken for notifications on 4.4, guess it was the other way around. At least this explains What has been going on with it.

    • andy_o

      FB messenger uses GCM, which should wake the app. I even greenify it.

      • Tony

        Just curious, but how'd you find out they use GCM?

        • http://www.androidpolice.com/author/cody-toombs/ Cody Toombs

          I'm not sure how andy_o noticed it, but I dug into the apk and found references to GCM. Facebook Messenger doesn't use it for actual messages, all of that is still happening directly over FB's own infrastructure.

        • andy_o

          Greenify tells you if they do. Also, it the fact that you don't stop receiving notifications after it's been greenified.

    • Tony

      Actually I've had friends tell me their FB messenger on their iPhone was skipping notifications as well. Might have something to do with FB themselves.

  • Roger Siegenthaler

    So nobody here is annoyed that google's apps aren't affected by this? I kind of find it a bit drastically evil on their part that their apps aren't pulled into this away, it forces the users to run to google's services. **cough monopoly misuse** we've seen this before.

  • Chandler Scarborough

    Am I the only one who thinks this is a GOOD thing? Far too many self-important apps think they need to stay active in the background. For most of the apps on my phone, I'd be thrilled to have them die until I manually start them. This feature alone would be reason enough for me to upgrade - if not for the other change that prevents apps from writing to the SD card. I guess rooting is becoming inevitable.

    • blast0id

      the problem comes when something you WANT running in the background, ie: alarm clock, tasker...etc, gets killed "accidently" and then you miss an appointment, or don't wake up on time because of it, then you'll be cursing, not praising... I do agree though it would be nice when you MANUALLY kill an app, if it would ask you if you would also like to kill that apps background services!

  • Fatal1ty_93_RUS

    Ah, so this is why my DashClock and Battery Widget Reborn are sometimes stuck in a limbo

    • Andro Tuning

      Try Battery Monitor Widget, it's not affected by this. Android Tuner have battery monitoring too and allows for setting it as a foreground service (with a useful notification icon)

      • Fatal1ty_93_RUS

        Meh, I dont like neither. Battery Monitor doesn't have quick toggles unlike BWR, and Android Tuner pakcs TOO much stuff I don't need inside

  • ithehappy

    Great, so to cover up a bug you write three meaningless and false lines at the start of an article to distract kids from getting into the actual point.
    Wait, everybody ain't kids?

  • Mick Collingwood

    I've had a problem with Viper4android where sometimes it stops processing and I'll have to open the app interface then it will run as normal. Don't know if it's related to this bug as it has a persistent notification which I thought would stop it from being killed.

    I have noticed this issue with a few apps though and I've had frequent crashes with Talon and Falcon Pro, since they 'listen' for mentions, etc. I wonder if their background service is being killed and causing the crashes.

  • Tess

    I want old version this 4.4.2 kills my battery faster , the phone its freezes How I can get old version Back???

  • ghjjgf

    can someone compile the new services.jar or whatever file it is we need?

    since it's been four months already, waiting for a fix from Google is just painful.
    .

    • Sakkarin Siramat

      Hi

  • bajicdusko

    I have googled this article. I am developing app with service which makes poll request on 15 second period. Everything works as expected when application is still in memory, but when i remove it from "recent" list, 15 second loop in service stops ..

    Curious thing is that service is not killed, and any method does not receive call (onDestroy, onCreate, onStartCommand).

    P.S. I have Log messages on every possible line, but after app kill any service activity is stopped.

  • max

    This happened on my phone that is currently running on 4.2.2 I'm using a lenovo a850+, everytime I swipe it away using the task manager via the home button, it kills every processes related to the app like facebook, now I have to keep the app alive to receive notifications because every app won't run on background after being swipe away. This is a headache, help me fellow internet comrades

  • Feisty

    Google has COMPLETELY ruined my phone. Every task on my phone is freezing up and everytime my phone locks up the only way i can regain function again is to go into the "cleaning app" ive installed and kill all the running Google apps such as "google play services, google±, google maps, google chrome, etc etc." I have to kill these Google services every few minutes otherwise my phone cannot be used. Everytime my phone locks up the task killing app shows that over %90 of my phones entire Ram and Cpu is being used up my these pesky Google services. So many times I have come close to smashing my phone, so many times I have uninstalled and disable every Google service only to find it has severe consequences on the function of my android phone and other apps such as photo album. Even typing this message I've had to restart over and exit 5 times to disable google apps as it keeps freezing my phone and leeching my phones entire resources. I despise Google beyond words. I've now decided to buy an apple phone before i lose my sanity.But in the meanwhile how can i fix this problem?