partial wakelock for 3G in Android - android

I have a service that uploads file to a server. I acquire a wifilock on it but do I need a partial wakelock if the service is using 3G ?

Yes. Otherwise, the device may fall asleep during your upload process.

WakeLock is an Inefficient way of keeping the screen on. Instead use the WindowManager to do the magic. The following one line will suffice the WakeLock. The WakeLock Permission is not needed for this to work. Also this code is efficient than the WakeLock.
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
You need not relase the WakeLock Manually. This code will allow the Android System to handle the Lock Automatically. When your application is in the Foreground then WakeLock is held and else android System releases the Lock automatically.

Related

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.

Is using WakeLock overkill while using WifiLock on Android?

My audio streaming app is working correctly with only WifiLock.
But some devices report that when they turn off the screen, connection is cut and audio streaming is topped. (They say this does not happen on 3G)
So, I thought maybe I can also use a Partial WakeLock. I am not sure if this fixes the issue but I think it is an option. At the same time, adding a WakeLock while a basic WifiLock is working may deplete the battery faster and may not fix the issue.
What do you think about adding a WakeLock too for this kind of issue and app?
private static WifiLock wifiLock = ((WifiManager) appInstance().getSystemService(Context.WIFI_SERVICE))
.createWifiLock((android.os.Build.VERSION.SDK_INT>=12?WifiManager.WIFI_MODE_FULL_HIGH_PERF:WifiManager.WIFI_MODE_FULL), "myappwifilock");
The newly added line:
private static WakeLock wakeLock= ((PowerManager) appInstance().getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myappwakelock");
(ofcourse I acquire and release these locks on the creation of the service and on the destruction of it.)
Use them both. The behavior I am sure varies from phone to phone. You may wish to search for the devices the reports are about + "wifi"or "wifi driver". Are you sure your audio streaming app is working correctly with only WifiLock ? This sounds very strange - the CPU will go to sleep and the service will stop - see Service, WakeLock. Something else is keeping the phone awake. So you need a wake lock
If you only use wake lock on the other hand the wifi will turn off maybe - I am not sure for you are using it - but better safe than sorry. If it does turn off waking the phone up won't wake it up - for this I am sure. Using the wifi lock has no impact on the battery - using the wifi radio has, and this you are doing anyway.
So both - and be sure your service acquires them - have a look at WakefulIntentService

How can I keep my Android service running when the screen is turned off?

When the screen turns off, my application service is paused.
I start my service with the following code:
if (mSharedPrefs.getBoolean("prefAutoUpdatesMain", false)) {
Intent svc = new Intent(this, MyService.class);
startService(svc);
}
How can I can avoid the service pause?
What I have to do in MyService is to download some data from Internet. If I have understand the process I have to follow is:
Acquire wakeLock
Download data
Release wakeLock
In downloading data method there are no reference to wakeLock, it is the application to have the wakeLock, is it correct?
Wake locks are reference counted by default. I think it is better a wakeLock without reference counting, to be sure to release it, am I wrong?
A partial WakeLock is what you want. It will hold the CPU open, even if the screen is off.
To acquire:
PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
To release:
wakeLock.release();
WakeLock also supports reference counting so you may have multiple things in your service that require wake functionality, and the device can sleep when none of them are active.
Things to watch out for:
If you use reference counting, make sure all control paths through your application will properly acquire/release...finally blocks come in handy here.
Also be sure to hold WakeLocks infrequently and for short periods of time. They add up in terms of battery use. Acquire your lock, do your business, and release as soon as possible.
You need a partial wake lock.
Detailed example here in a previous question:
Wake locks android service recurring
I'm just using a foregrgound service.

prevent mobile from going into sleep mode when app is running

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.

Android: Enumerate all wake locks in the system

I have trouble with an Android app keeping a partial wake lock on my system; so I'd like to write an app that keeps an eye on the bad app and kills its partial wake lock shortly after it acquires one.
android.os.PowerManager is used to acquire and release wake locks but I don't see any way to get all wake locks in the system; even if I got access to its IPowerManager mService variable, that service doesn't release a wake lock without the IBinder object associated with the wake lock.
So I'm stuck. How do I proceed? Is this even possible?
It's not possible, in that you cannot write an app that cancels another app's WakeLocks.
FWIW, you can use adb shell dumpsys to find out about outstanding WakeLocks, among other things.

Categories

Resources