prevent mobile from going into sleep mode when app is running - android

I want to prevent Android Mobile from going into sleep mode when my threads are sending HTTP request. as what happened while threads are doing HTTP calls mobiles goes to sleep mode and when user wakes up the phone threads never complete.
User has to restart the app. what to do? please help

You need a WakeLock. There are different kinds of wake locks so be sure to select the least aggressive one that meets your needs. In particular it sounds like you need a Partial Wake Lock.
Partial Wake Lock - Wake lock that ensures that the CPU is running. The screen might not be on.
Also, make sure you add the permission android.permission.WAKE_LOCK to your manifest. And finally be double sure to Release your lock when you are done.

To prevent the phone from sleeping you can use a WakeLock but you should be careful when doing this to not kill the user's battery. If the phone goes to sleep does the user really care if your app finishes the requests it was making?
You will also need the WAKE_LOCK permission.

for JAVA: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
for KOTLIN: window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Use this code, it will keep your device wake up and you didn't need any permission.

Related

Services and wake lock

I have some questions concerning wake lock and services
1- I try to test my service when the screen is off and not acquiring the wake lock, i was expecting that system will kill my service but it didn't happen, so what is the purpose of wake lock?
2- I want to know when then the system goes into doze mode, is it when i turn off the screen or after some time of turning it off? and what happens to my service in this case? and how to know that the system is in doze mode?
3- I know that since Android O normal background service will be killed after nearly one minute, i tried to test that by making intent service and make it running for more than one minute, it was already killed but started again and continued execution, so what is the purpose of killing it and starting it again?
4- does doze mode affect foreground service? and should i acquire wake lock in case of foreground service or is it acquired by default?
Code of Intent service
Logcat
I know they are lots of questions but i am confused with these topics
thanks in advance
The device may fall asleep if the user is inactive and nothing is keeping the device awake. A WakeLock is used to ensure the device stays awake.
You may check those links for additional information: Good answer Official Documentation
Information about Doze mode, Standby and some other things that you may be interested in:
link
Background Service Limitations: While an app is idle, there are limits to its use of background services. This does not apply to foreground services, which are more noticeable to the user.
link
Processes which have a current running foreground service are supposed to be unaffected by Doze. Bound/unbound, started/not-started, and wakelocks do not affect this whitelisting process.
link
--- Update ---
Some things could change from the moment of those questions were asked, so prefer to read documentation or search for the actual information about it. Also it's a good idea to check information about modern solutions for back ground like WorkManager.

Android: When is WakeLock needed?

If I have an IntentService that simply updates the SharedPreference, is a (partial) WakeLock needed?
I understand that a WakeLock keeps the CPU awake, but when is it needed?
If you need to keep the CPU running in order to complete some work before the device goes to sleep, you can use a PowerManager system service feature called wake locks. Wake locks allow your application to control the power state of the host device.
Creating and holding wake locks can have a dramatic impact on the host device's battery life. Thus you should use wake locks only when strictly necessary and hold them for as short a time as possible. For example, you should never need to use a wake lock in an activity.
One legitimate case for using a wake lock might be a background service that needs to grab a wake lock to keep the CPU running to do work while the screen is off. Again, though, this practice should be minimized because of its impact on battery life.
Unfortunately, some poorly-coded, malicious, or simply buggy apps might create an abnormal amount of undesirable wakelocks. Other apps require constant Internet access in order to operate in a normal fashion - Facebook and Messenger are probably the most popular representatives. They persistently request information from the web (the so-called "polling" for new events), which is causing subsequent wakelocks.
In other cases, an update to a given app can also cause certain issues, which usually result in partial wakelocks. The latter keep your CPU constantly humming in the background, sometimes without your knowledge, and prevent your device from "going to sleep". That's a pretty substantial prerequisite for anomalous battery drain. Thus, it is advisable to regularly monitor the wakelocks on your device and see which of your apps go harsh on our system's resources.
Read more at:
What-are-wakelocks-how-they-affect-the-battery-life-of-your-Android-device-and-how-to-Greenify
Reference: https://developer.android.com/training/scheduling/wakelock.html
It is needed when you don't want CPU to sleep when user locks the screen for example.
If you have an IntentService without acquired WakeLock it will pause after a while if user locks the screen and it will continue its work when user wakes a device. With WakeLock acquired your service will work even if the screen is locked.
As #My God mentioned, it impacts on battery life a lot, so, use it only when you really need to finish some operation and you cannot wait till user wakes a device.

When and why PARTIAL_WAKE_LOCK is required?

All other wake locks makes sense e.g. developer want screen not to go off while performing something. But in partial wake lock documentation says that :
If you hold a partial wake lock, the CPU will continue to run,
regardless of any display timeouts or the state of the screen and even
after the user presses the power button. In all other wake locks, the
CPU will run, but the user can still put the device to sleep using the
power button.
Does that mean while performing some operation CPU can go off? Is it required to acquire wake lock in a service?
Does that mean while performing some operation CPU can go off? Is it required to acquire wake lock in a service?
Of course (and here - for the power off button) ! Things are even more complicated if you are trying to start your service while the device is asleep. You most probably won't make it. Have a look at Commonsware WakefulIntentService - the notion is that using an alarm manager Receiver (which holds a wake lock) you must afterwards get a (static) wakelock while still in onReceive() to keep awake.

Background service not running when screen is off

I am using WakeLock but it consues a lot of battery. Is there any alternate solution??
The whole idea of WakeLock is to allow the device to run some code instead of going into sleep mode. And whatever way you're using to prevent device from sleeping, its going to consume battery. As far as I know the only way to run code while "screen is off", is to use WakeLock.
Android OS is very aggressive about power consumption, and unless you explicitly request to run you application when device is going to power save mode, it will "pause" your app process.
So, if you think you must run your code all the time, you should accept battery drain. If app drains too much battery, then consider changing your design and/or decision about how often and how long your app runs, especially when device tries to enter power saving mode.
you can implement thread inside service with infinite loop but when you stop service don't forget to release all the resources you have used in service as android does not do it for you.
try to use Async Tasking:
http://developer.android.com/reference/android/os/AsyncTask.html

Android: Disabling or at least ignoring standby

I'm writing an app with alarm functionality, i.e. it will have to continue running while the device goes into standby. Apparently, this is not the case. How can I achieve that?
It would be even better if I could prevent the device from going into standby at all - can I do that?
Android applications can prevent the device from going into standby with a wake lock but this should be used only for short periods of time for specific tasks which require the user to look at the screen without touching it.
For any other long time purposes, you have to use the system AlarmManager to schedule future actions of your application.
Using a permanent wake lock would make your app a battery drainer.
A service runs always, even in standby, so you should use it.
Preventing standby can be done with a wake lock. Be careful, there are only a few apps that should prevent standby (like games, video players...).

Categories

Resources