- Keeping your Android application running when the device wants to sleep (Updated for Android Pie)
- Wake Locks
- Wake lock acquired.png
- WiFi Locks
- How to stop apps from running in the background on Android
- Jump to
- Why stop background apps on Android?
- Update your phone to the newest Android version
- Analyze battery consumption statistics
- Control your background processes
- I’m in! But I don’t know which apps to stop
- Task killers and RAM optimizers: the great debate
Keeping your Android application running when the device wants to sleep (Updated for Android Pie)
Updated January 2020 to include detail about Android Pie.
Since the introduction of doze mode in Android Marshmallow we have seen a number of enterprise use cases where customers want to ensure their application continues to run even though the device wants to enter a power saving mode. It is assumed the reader is familiar with doze mode and the other changes made in Android Oreo and Pie related to background services, at least at a high level.
From a consumer point of view, getting maximum battery life out of a device is frequently an ever-present consideration, so much so that a slew of snake oil “task manager” and “task killer” applications formerly gained popularity to prevent background apps but in recent Android releases Google has taken a more aggressive approach to what apps can do in the background.
Enterprise applications are written primarily to enhance user efficiency; battery consumption will always be a consideration but may be secondary or tertiary to application responsiveness or performance.
The goal of this blog is to explain your options as an application developer to give you the most control over your device’s power management and what you can do to ensure your application is always available to your users.
There are two fundamental reasons people might not want the device to enter a power saving mode:
- The application should be available to respond to network requests, for example a push message. Often Firebase Cloud Messaging (FCM) is not a suitable option for customers because the device is behind a firewall or does not have GMS services available.
- The application needs to do work continually and it is not acceptable for the Android OS to kill the application’s background services.
Before diving into the detail, it is necessary to understand the concepts around Android power management:
Wake Locks
Wake locks are an Android concept designed to allow an application to indicate that it wants to have the device stay on, for example the YouTube application would take a FULL_WAKE_LOCK to prevent the screen turning off whilst the user is watching a video. A wake lock can control the status of the CPU, Screen and hardware Keyboard backlight but all but one type of wake lock can be cancelled by the end user simply by pressing the power key. Since only PARTIAL_WAKE_LOCKs persist when the user presses the power key the remainder of this blog will be concerned exclusively with those, where the CPU continues running but the screen and hardware keyboard backlight is allowed to turn off.
Android documentation is available for the wake lock definition as well as methods to acquire and release wake locks.
Any application requiring a wake lock must request the android permission:
You can detect any wake locks being held by applications on your device through adb:
The following screenshot shows a single wake lock is on the device, it is a partial wake lock and has been given the tag ‘WakeLockExampleAppTag1’ by the application which created it:
Wake lock acquired.png
Partial wake locks will be released either when the application which created it calls release(), or the lock was only acquired for a specified time. Note that wake locks will be automatically released when the device enters doze mode unless battery optimization is disabled for the application which acquired the lock (see later)
Whilst this blog is concerned primarily with Android Marshmallow, you may notice that earlier Zebra devices’ WiFi service gains its own partial wake lock, even when the WiFi policy is set to not «keep WiFi awake when the device is sleeping». You can see any wake lock using the technique described above and should bear this in mind if wondering why your device is not sleeping when expected on earlier devices e.g. MC40.
WiFi Locks
WiFi locks are only applicable to Android Nougat devices and below (Marshmallow, Lollipop etc). From Android Oreo onward the WiFi will always be on when the device is sleeping.
WiFi locks allow an application to keep the WLAN radio awake when the user has not used the device in a while, they are frequently used in conjunction with wake locks since any application doing work in the background would likely need WLAN connectivity (e.g. downloading a large file)
Android documentation for the WifiLock is here and the documentation on how to acquire a WiFi lock is here.
As stated in the official docs, WifiLocks cannot override the user-level “Wifi-Enabled” setting, nor Airplane Mode. For Zebra devices, we can extend this to WifiLocks not being able to override the Wi-Fi Enable / Disable setting of the WiFi CSP
Some other special considerations for Zebra devices:
- Out of the box, older Zebra devices come pre-loaded with the AppGallery client. AppGallery will be holding its own WifiLock lock and since the radio is only allowed to turn off when no WifiLocks are held, the radio will not turn off by default (until the device enters doze mode). AppGallery’s lock is defined as follows:
- WifiLock
- You can disable AppGallery in a number of ways:
- Using the MX AccessManager whitelist feature
- Using the MX ApplicationManager to disable the application
- Using Enterprise Home Screen’s preference
- Disabling the application manually under Settings —> Apps —> AppGallery —> App info
Note that the current package name is com.rhomobile.appgallery but that may change in the future.
- Zebra devices also have an additional “Sleep Policy” parameter as part of the MX Wifi Manager
- You can set the sleep policy via the Settings UI: Settings —> Wi-Fi —> (Menu) Advanced —> “Keep Wi-Fi on during sleep”. The values are:
- Always (the WiFi radio will not turn off when the device sleeps)
- ‘Only when plugged in’ (the WiFi radio will not turn off provided the device is connected to power)
- Never (the WiFi radio will be allowed to turn off when the device sleeps)
- The sleep policy does not hold a separate WiFi lock, it is configuring the WiFi policy and is reflected in the mSleepPolicy value.
- The default value of the sleep policy will vary from device to device. As a general rule, devices running Marshmallow or earlier will have this value set to ‘Never’ and devices running Nougat or later will set this value to ‘Always’ but there will be exceptions (notably the TC8000, a Lollipop device, defaults to ‘Always’).
- For a consistent staging experience across all devices it is recommended to use StageNow’s WifiSleepPolicy parameter
- The device will only turn WiFi off during standby if there are no WiFi locks and it is allowed to do so according to the WiFi sleep policy
- You can set the sleep policy via the Settings UI: Settings —> Wi-Fi —> (Menu) Advanced —> “Keep Wi-Fi on during sleep”. The values are:
- When the device receives a network request over WiFi, the device obviously needs to do some processing to handle the message. Although this requires hardware support from the WiFi stack and processor, recent Zebra devices will be able to wake the processor to perform the required packet handling. If more than simple processing is required when packets are received it may be prudent to also acquire a wake lock for the duration of that processing. This is true of all Android Marshmallow or later devices.
You can detect any wifi locks being held by applications on your device through adb:
The following screenshot shows a single wifi lock is on the device (in blue) and is given the tag ‘WifiLockExampleAppTag1’ by the application which created it. Highlighted in green is the mSleepPolicy variable which can be used to determine the WiFi sleep policy, here 0 is ‘Never’ keep WiFi on during sleep but a value of 2 would indicate ‘Always’ keep WiFi on during sleep.:
Источник
How to stop apps from running in the background on Android
Android smartphones have gotten very powerful over the years and are great at multitasking. Over time, however, you might have seen smartphones degrade in performance and become sluggish. You may also notice that the battery life is also adversely affected. What if we tell you that on most phones, these issues can be fixed by simply optimizing the background apps? Read on to find out how!
Jump to
Why stop background apps on Android?
For those of you who chanced upon this article without actually facing an issue with their phone, you might wonder why would someone even consider to stop background apps. Here’s an explanation.
As you keep using your Android phone, you tend to install several apps on your device. Over time, chances become high that at least some of them aren’t well optimized for your phone or may have a bug. Some of these apps may even contain malware – especially the ones you did not download from trusted sources.
Oftentimes, these apps may keep running in the background even after you thought you closed them. The result is sluggish performance and a marked reduction in battery life. Let us now check out the various steps that can be taken to solve this problem and stop background apps on Android.
Update your phone to the newest Android version
In case you have a relatively old phone with you, the simplest way to stop apps from running in the background is to make sure your phone runs the latest version of Android. Starting Android 10, Google has introduced several power management features that prevent errant apps from consuming too much power or hogging too much power.
Android 11 is the latest stable version of Android and provide great battery optimization tools/ © Mr.Mikla / Shutterstock.com
If you are on a newer version of Android (the latest stable version is Android 11) you are already doing pretty good as Google has enhanced power management features even further. On phones running Android 10 and 11, it would be a good idea to turn on a feature called Adaptive Battery. On phones running stock Android, you can enable Adaptive Battery by going into Settings -> Battery -> Adaptive preferences.
Apart from optimizing errant apps, the Adaptive battery mode also optimizes the charging speed of your phone to ensure that your battery lasts a long time.
Analyze battery consumption statistics
There is no denying that Android offers a comprehensive battery statistics tool where you can see what apps are draining the battery the most. To access the battery stats page tap on Settings -> Battery ->View battery usage.
The phone will now open a page with the list of apps that are consuming power. What is helpful about this page is that it lists these apps in an order — with the ones consuming the most power placed at the top. Chances are high you will see some of your most used apps high up in the list. However, if you note some third party apps that you know you haven’t used for a long time in this list, it might be a good idea to either force stop or uninstall it.
The Battery stats page is a veritable goldmine of information/ © NextPit
To force stop or uninstall an app, tap on the app and and you will see a page with both these options. Note that you may also see some apps here that are part of Android and Google Mobile Services. You can ideally leave them be since they are essential for the smooth and stable functioning of your phone.
Control your background processes
Incase you are not too happy with the results of disabling apps listed within the battery stats menu, it’s time to try out slightly advanced steps. The best way to gain control of your processes is to have a dig around under the hood using the monitoring tools built into Android.
In some cases, before you can start, you need to enable developer options.
- In most versions of Android, this involves going to Settings > About phone and then tapping Build number about seven or so times. You’ll get a notification telling you that Developer options have been unlocked once you’re done.
- For many handsets, the next thing you need to look for is a setting called Processes, Process Stats, or Running services. You can find this in Settings > System > Advanced> Developer options > Running services. That option takes you to a list of running processes showing how much RAM each is using.
- Obviously, it’ll be tempting to stop the most RAM-hungry apps from running in the background, but you’ll want to pay some attention to what you’re stopping before you go ahead. Stopping some apps might crash your phone.
- You can also tap the settings option when in the Services/Processes menu to switch between running processes and cached processes.
Control Android services from within Developer options / © NextPit
I’m in! But I don’t know which apps to stop
OK. If you don’t want your app/phone to crash, err on the side of caution and use some common sense. The app marked ‘Google Services‘, or pretty much any app that starts with ‘Google’ shouldn’t be manually stopped.
On the other hand, if you look through the list and see messengers and music players idly draining your battery in the background, then you can go ahead and stop them in relative safety. Truly crucial apps usually won’t allow you to force stop them anyway.
- To stop an app manually via the processes list, head to Settings > Developer Options > Processes (or Running Services) and click the Stop button. Voila!
- To Force Stop or Uninstall an app manually via the Applications list, head to Settings > Apps > All Apps and select the app you want to modify.
- Don’t delete any apps that appear when you select the Show System Apps option. You can see system apps by tapping on the three dots on the top right corner.
Apps that you rarely use are better off uninstalled altogether. / © NextPit
Task killers and RAM optimizers: the great debate
With Android software and hardware improving over time, some people will argue that the use of task killer apps is going to do more harm than good in terms of processing and battery life. As one of the problems you’re trying to fix is apps running in the background draining your resources, adding another one that has the purpose of doing explicitly that (it needs to monitor the services in use on your phone, therefore always needs to be running) seems a bit counter-intuitive.
A task killer that repeatedly force-closes an app in the background over and over is almost certainly going to drain your battery more as it continues the ‘restart and kill’ process. You might be better off not installing the task killer in the first place and just letting it run.
So there you have it! We have discussed several ways in which you can can stop background apps from running on Android devices. We hope the steps mentioned here proved helpful for you. In case you know if any other cooler methods that we might have missed out, share them with us in the comments below!
This article was comprehensively updated in July 2021. Older comments have been retained.
Источник