i've got a problem with my Android FileObserver.
I have a background service running (returns START_STICKY), which references two FileObservers.
Everything is working fine until the device is set to sleep mode, which will stop the service, as far as i know.
Does anybody know what exactly happens to the Service/FileObserver when a device is put to sleep mode?
Can the service get notified before freezing so I can save a file list of the observed folder and compare it to a new list when the device gets waked up again?
I don't want to use a wakelock because of its impact on battery life.
Will the FileObserver Event be fired for changes to folders which happend while my service was set to sleep, as soon as I turn the screen back on?
That can't really test it because usb debugging keeps the device awake.
Thanks for your help!
Everything is working fine until the device is set to sleep mode, which will stop the service, as far as i know
No, it stops the CPU.
Does anybody know what exactly happens to the Service/FileObserver when a device is put to sleep mode?
The CPU is stopped. Stopped CPUs will not execute instructions. It is reminiscent of putting your development computer in suspend mode.
(the details are significantly more complicated but usually are not relevant at the SDK level, IMHO)
Can the service get notified before freezing so I can save a file list of the observed folder and compare it to a new list when the device gets waked up again?
No.
Will the FileObserver Event be fired for changes to folders which happend while my service was set to sleep, as soon as I turn the screen back on?
I haven't tried that.
That can't really test it because usb debugging keeps the device awake.
Then unplug the USB cable. Use Log statements to record the results that you wish to monitor. Plug in the USB cable after your test period to examine LogCat and see your logs.
Generally speaking, your overall solution (continuously-running service) is user-hostile. Please don't complain when users attack you with task killers and the Force Stop button.
Related
I have created an app which will start a service. In my service, I created a timer that will do something periodically. I didn't use any wakelock.
After I press the power button and wait for sometime, it seems that my service still keeps printing something periodically into the eclipse( I keep the usb connected in order to see the message in eclipse).
So my service is still running and will never go sleep even the power button is pushed?Then it will keep draining the battery? Is that caused by the usb connection? Does anyone have such experience?
Keeping the phone plugged in prevents it from going to sleep. A better test would be to disconnect it from adb, wait until a couple of the timer intervals have passed, and then plug the phone back in and check logcat. I don't recall offhand if eclipse shows the time associated with each logcat message. If not, you can use adb logcat -vtime to get this information.
I am developing an android application and I want to reduce the power consumption. The method I believe is to put the phone into sleep mode whenever the user activity stops for a certain threshold period. I have three questions regarding this.
If I release the wakeLock and no other application is holding the wakeLock after how much time would the phone go to sleep?
I have multiple HandlerThreads running where I use sendMessageDelayed() function. Would these messages get delivered even after the phone goes to sleep mode?
Does putting the phone into aeroplane mode save more power rather than just putting the phone to sleep. if yes, then why is it because the only difference in those two modes is the use of cellular network.
If I release the wakeLock and no other application is holding the
wakeLock after how much time would the phone go to sleep?
There really is no definitive answer, but, from personal experience, I'd say it is likely that it will happen within 30 seconds to 1 minute.
I have multiple HandlerThreads running where I use
sendMessageDelayed() function. Would these messages get delivered even
after the phone goes to sleep mode?
I really wouldn't count on it because I've never seen anything that says it will wake up the device to send said Message. You can always test it, but I wouldn't trust it to work because the documentation does not claim that it will.
Does putting the phone into aeroplane mode save more power rather than
just putting the phone to sleep. if yes, then why is it because the
only difference in those two modes is the use of cellular network.
If you put it into sleep mode AND airplane mode, then you will save more battery than JUST sleep mode.
The reason for that is that even with the CPU pretty much asleep, the phone must keep a constant connection with the cellular network in order to know if you get a text or phone call. To do this, it must use the battery to constantly keep the antenna turned on. If you put it into airplane mode, it would basically turn the antenna off, and then the phone would not be using battery for that function.
Is there a notion of sleep stages/levels on Android?
From browsing the mailing lists, I'm aware that there exist a stage called "Deep Sleep". Do execution for all apps halt when device reaches this state? If so, besides user hitting the power button, what else could wake the device back up?
From browsing the mailing lists, I'm aware that there exist a stage called "Deep Sleep".
There is not really a separate stage called "deep sleep". There is only "awake", "asleep", and "off".
Do execution for all apps halt when device reaches this state?
Execution of all processes ceases when the device goes to sleep or is powered off.
If so, besides user hitting the power button, what else could wake the device back up?
An alarm from AlarmManager
An incoming phone call
An incoming text message
If you have a socket open on wireless data (not WiFi), an incoming packet on that socket
Those are the big ones. There might be others.
I've noticed the following behaviour:
You have your activity open and stop interacting with it
After a few seconds (it depends on how the device is configured) the screen will go off.
When the screen goes off, onSaveInstance and onPause are called.
A few seconds later (usually ~15s) the device enters into sleep mode (is this the correct name?)
When this happens, the following methods are invoked: onStop (calling isFinishing returns false), onRetainNonConfigurationInstance and onDestroy.
So far so good. Now, the strange behaviour begins: just after the last onDestroy finishes, another activity is created: onCreate, onStart, onRestoreInstanceState, onResume and finally onPause are invoked.
I find no reason for this strange behaviour. Why would another activity be created just to go straight to pause mode? This happens immediatly after onDestroy of the original activity!
This was tested on Galaxy S. I didn't test what happens after a few hours with no activity. I'm not sure if anything else will happen.
I hope this will help you.
A short addition to the commonsware's list. After looking for a way to run methods periodically while phone is asleep, I've found out that TimerTask functions during sleep mode.
TimerTask is, in my experience, easier to work with if all you want is to run methods from a service and not to start an activity.
In Android API 23 the way 'sleep' works was been changed. They have added Doze and App Standby. You can read about both of them here.
Doze: This would be 'sleep'. A few minutes after the screen shuts off the phone will enter this mode shutting down all network connections. Then at certain intervals (maybe a linear back-off policy for example) the apps will be 'allowed' to access the network for ~10 seconds. There is no real way around this if you want to publish your app to Google Play outside of using Google FCM. It might also be worth noting that uptimeMillis is not guaranteed to be updated during Doze because the CPU can enter deep sleep mode (elapsedRealtime will still be accurate).
App Standby: This will essentially stop your app if the device determines that the app is 'idle'. An idle app is a state that is determined by these factors
Has the app been launched by the user?
Has the app run a foreground service?
Has the app generated a notification?
Is the app an active device admin app?
If the answer to all of these is no, the app will be set to 'idle' and have greatly restricted network access (allowed once a day and/or while charging only). I am not sure how long an app must meet these criteria. However, it seems to be at least a few days before the App Standby state will be entered.
Bonus: Device States (managed by DeviceIdleController)
ACTIVE - In use, or connected to a power source.
INACTIVE - Device has come out of the active state (user turned off the screen or unplugged it)
IDLE_PENDING - About to enter idle mode.
IDLE - Device is idle (Different than an app being flagged as idle from App Standby. This is the entire device.).
IDLE_MAINTENANCE - Open for applications to do processing (10 second window).
If you want a background service or worker to check if the app is in the IDLE state then you can use the function isDeviceIdleMode (only works when entering Deep Doze, see below).
Example
(getSystemService(Context.POWER_SERVICE) as PowerManager).isDeviceIdleMode
Some more complications
API 24 added more complexities to the Doze mode (Light Doze and Deep Doze). This essentially puts nested states inside the device states.
API 28 added "Adaptive Battery" prediction, which makes use of Doze to hibernate user apps the OS determines the user will not use.
API 28 also added "App Standby Buckets" to add more states to App Standby than just idle and active.
Besides the "awake", "asleep", and "off" states that #CommonsWare mentioned, there is the distinction between whether the CPU is asleep, or just the screen is. For example, the official docs here describe it this way:
To avoid draining the battery, an Android device that is left idle
quickly falls asleep. However, there are times when an application
needs to wake up the screen or the CPU and keep it awake to complete
some work. [emphasis added]
In the three-stage framework that CommonsWare described, a device whose screen is dark is probably not categorized as "asleep" unless the CPU is also stopped. But as the above paragraph implies, the screen-dark state can legitimately be referred to as "asleep." No doubt this is why people refer to "deep sleep" to clarify that they're talking about the CPU being asleep.
This doc page also mentions
When an Android device is left idle, it will first dim, then turn off
the screen, and ultimately turn off the CPU. This prevents the
device's battery from quickly getting drained.
So if you want to be comprehensive, you could add "dim" to the list of "sleep stages/levels":
awake
dim
screen off
CPU off (true "sleep" or "deep sleep")
power off
Apparently the transition from 2 to 3 to 4 is pretty fast when the idle timeout occurs. But there are other times when the screen can be off without a transition to deep sleep; e.g. when playing audio (at least in certain apps).
I wish I could tell you how to predict when the device will transition from screen off to CPU off -- e.g. how long the timeout is -- but I haven't found that information. What I have found is FLAG_KEEP_SCREEN_ON and WAKE_LOCK to prevent one or the other from happening.
P.S. If you want to be exhaustive, you could count daydream in your list of "sleep stages":
Daydream is a new [as of Android 4.2] interactive screensaver mode for Android devices. It
activates automatically when the device is inserted into a dock or
when the device is left idle while plugged in to a charger (instead of
turning the screen off).
From the point of view of the previously-running app, it sounds like daydream behaves like switching to a different app. So it's not really a matter of the device sleeping, though your activity does get stopped, I would assume.
I’m doing an application that listens to the android.intent.action.SCREEN_OFF in a Service (if that matter) and then it is supposed to wait a few seconds and launch an action, I’ve tried a timer schedule method, thread and handler postDelay method but all of them seems to fail, they are never executed on a device, it seems like it’s being freezed/killed after phone is locked. It works on emulator and on device attached to USB, but never with device working on battery only, which actually is a main scenario.
Do you know any solutions to this?
I cannot test this now (not near my Android device at the moment), but...
Does the broadcast receiver fire at all?
If so, right as you get called, you will need to grab a partial Wake Lock so the CPU stays awake until the timer fires.
chances are you have your phone set so that when it is plugged in, it never actually locks or sleeps (its one of the developer settings) and imagine the emulator behaves the same way. so you will either have to override the phone's lock settings or change your method. the general rule is that when the screen shuts off, your activity ends.
A partial wake lock keeps the CPU running, so you should acquire it inside your broadcast receiver for screen_off, and release it after you're done with whatever you're doing. When you're plugged into the USB, the CPU stays on to service the USB connection (in fact, the usb stuff in the kernel holds its own wake lock).
And to answer your other question, yes, even background threads. As soon as everyone is done handling the screen_off broadcast, the CPU is turned off, and you won't run again until someone turns the CPU back on.
I'm creating a datalogger which logs the gps data and the sensor data of the phone. I've just a strange problem. If the phone is not touched for few minutes it goes into standby mode (screen goes black), and that causes the logger to stop working. Actually the firewriting in the logger stops working. The GPS resumes fine after coming out of blackscreen, but when I check the log I created it only shows saved points for few minutes.
Does anyone have idea what is causing this?
This is normal behaviour to save battery. Unless the phone is explicitly told to 'wake up' by your program, everything is stopped while in standby mode. I haven't experimented with this before so can't give any code, but I suggest you look at the Alarm Manager. However, there are two things to note: First off, this will kill the battery in no time if you have it log too often. You must be very careful implementing this alarm and if necessary the associated WakeLock. Secondly, you must make the user aware that it will continue logging even during standby, so that they aren't surprised when the battery is much lower than when they left it.