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
Related
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.
I have service running in android to sync files when user wants.
This is not a 24/7 service, only runs for a period of file transfers over wifi, so the user when done transferring files quits the app and therefore the service exits.
So the scenario could be that user has left the mobile and it might get locked/screen off automatically.
Regardless of screen on or off is there a way to ensure wifi is always performing the same with low latency ?
There are multiple apis in relation to this, Wifi lock, Wifi-sleep-policy, screen lock .. People seem to use a combination of them to keep good wireless performance .
Is wifi lock enough to ensure it ? or do I need to use combination of APIs ?
Thank you.
EDIT: this post was helpful
PARTIAL_WAKE_LOCK vs SCREEN_DIM_WAKE_LOCK in download thread
To be on the safe side, a WifiLock as well as a WakeLock would be optimal, with the WakeLock being the more important lock to aquire. The WakeLock ensures the device stays on, and the WifiLock ensures the radio is operating. This should ensure consistent latency. However, you can't expect anything when it comes to data transfers so have your app be ready to handle random connection losses.
I am relatively new to Android, so what I am asking may seem obvious (although I have read all the similarly titled questions, and have searched extensively). I need to monitor the accelerometer continuously for long periods. Two approaches have been suggested:
1) acquire a partial wake lock that is held the entire time the acceleromtere is being monitored; and
2) monitor the accelerometer in a foreground service.
The first approach appears to use a lot of battery life. The second approach should result in a service that is only killed rarely, but I'm not sure what "rarely" means. Which approach should be used, and are there alternatives that I should consider?
Holding a WakeLock and a foreground Service are not really related and shouldn't be compared are to which direction is best.
Android OS is built to swap out processes based on a variety of factors. This means your process might get killed at any point by Android and it provides a framework to help you, the developer, to ensure your app can save and restore its state when this happens.
A WakeLock simply prevents the CPU from sleeping which helps save battery when the phone is not in use.
Now, a combination of both would help you achieve what you want but at great user cost. I wouldn't want an app in my phone to keep the CPU constantly running or a notification icon to show up constantly in the notification bar (that's what a foreground service does).
Keep in mind, starting a service in foreground mode does not guarantee your app will not get killed. It might still happen albeit rarely.
What is it you are trying to achieve here? Why keep monitoring the devices accelerometer? Perhaps you should only monitor it only when an Activity of your app is in the foreground instead.
I had exactly the same need and problem. I believe the solution is to use both a partial wake lock and a foreground service. Android will try not to kill a background service that holds a wake lock but is free to kill it when it needs the resources and possibly restart it later. That's fine for a lot of purposes but at least in my case that is not good enough. Putting a service into the foreground state is the way to tell Android that that killing it is unacceptable. Yes, it might still happen in extreme situations but that would now be a violation of the API contract whereas with a background service Android is free to kill it. You should therefore probably code as if that that will never happen but just know that this is a possible but probably rare error.
This question sounds so easy I can't believe I can't find information on it (maybe I have the wrong key words in mind...)
I'm looking for a way to change the delay before the phone is put to sleep when my app is running.
I was using wake locks until now to prevent my app from being closed to frequently (its and opengl app and the loading time is a killer specially on slow phones).
I don't like the idea of indefinitely leaving the phone on (mainly because it empties the battery fast to have a full opengl app running).
Is there a way then to change the delay before the phones goes to sleep to 2~3min ?
I would also like to add that I do not wish to change the settings of the phone (that is only the users decision)
Jason
edited to correct the term "application going to sleep" which was pointed out as being incorrect.
Create a Timer object and release() your wake lock in the timer task. I'm relatively sure you can release a lock in a worker thread, but worst case, you'd need to use runOnUiThread if I'm incorrect.
Be sure to handle the case when onPause() is called and cancel your timer in that case and release the lock immediately.
In general, I'm not a fan of wake locks, but if this is to make your boss happy, wake-lock away.
EDIT: Btw, the system setting for the UI timeout is accessible by apps: SCREEN_OFF_TIMEOUT in android.provider.Settings.System. But really, this is for private applications only, public applications have no business changing this value.
If it is possible to keep an Air for Android application alive during sleep, how can it be done? I know how to prevent sleep and lock but this course is not preferable to some users.
Update
The reason in this case is to keep the video streaming from the phone to a server.
I think what you are talking about is preventing Screen Dimming for AIR apps. That is possible using the following:
NativeApplication.nativeapplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE
This overrides the default behaviour and screen never times out until the AIR app is in foreground. To take the mode back to normal you can set the property back to SystemIdleMode.NORMAL. In addition to this code, you will have to specify two permissions in your app.xml for AIR app, which are:
android.permission.WAKE_LOCK
android.permission.DISABLE_KEYGUARD
But as Emmanuel mentioned above, this would definitely mean that your battery will be consumed much faster. However, it could be useful in apps where Video is playing and you don't want the screen to dim.
Hope this helps.
Mohit
Why would you like to keep it alive? If the user no longer looks at his phone, why consume the CPU/RAM/battery?
If you want to do something in the background at regular intervals you could use an Alarm. http://developer.android.com/reference/android/app/AlarmManager.html
This lets you start the alarm even if the phone is sleeping.
If you want to do other kinds of background processing, you could use a Service running in the Foreground. http://developer.android.com/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29
This doesn't 100% guarantee that the service will always stay up and running though, e.g. when the phone runs low on memory.
I hope this is helpful...
Emmanuel