Android countdown timer application - android

I have countdown timer ticking in service. It updates UI through broadcast.
All works good, but I don't know how to wake device if it were in sleep state (user pressed the power button or enough time elapsed)?

Maybe you should use wake lock to prevent CPU to go to sleep, look at this question
Wake locks android service recurring and this How can I keep my Android service running when the screen is turned off?

Related

Long Running Timer Service Pauses When Device In Deep Sleep

I am developing an app that requires a long running timer service that calls a function after few minutes to send some data to the server. Its a foreground service running in the background on a separate process. I need the service running continuosly in the background until the user presses a button. But the problem I noticed is that the service pauses when device is in sleep mode which is not acceptable as the app is very time critical. Is there any way to make sure the service runs continously in the background? Is partial wake lock a good way to tackle this problem? Please help me as I'm new to android.

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.

Android service behavior is different in debug mode and release mode when use sleep button, why?

I am developing an application that uses a Service as Contdown. When the user starts the countdown from the activity and the activity goes in background after the sleep button is pressed, I am using this Service to continue the countdown. When the count is finished the Service shows a notification with ringtone.
I use wait() "to count" the time in the Service. The strange behavior occurs when I use the application on a real device, but in debug mode. When Eclipse debugger is attached, the Service works well; when I test the application on the device without Eclipse debugger attached, the Service doesn't show the notification when the countdown is finished, unless the sleep button is repressed and the monitor is activated - then the notification and the ringtone are activated.
Can anyone can explain what causes this strange behavior? Maybe the issue is is related to Wake lock or a similar construct?
I use wait() "to count" the time in Service.
That is poor programming practice. Time elapses even without your tying up RAM to do it.
Anyone can explaine why this strange behavior?
The device fell asleep. This is normal, and desirable, behavior, to conserve battery life. With the USB cable plugged in, the device does not need to fall asleep, and if you checked the appropriate option in Developer Options, the device specifically will not fall asleep while plugged in.
Maybe is connected with Wake lock
Please do not use a WakeLock to keep the device awake for you to watch the clock tick by. Please use AlarmManager to get control when the countdown period is over. You can use a _WAKEUP-style alarm to arrange to wake up the device, and your BroadcastReceiver that gets control at that point can "launch a notification and ringtone". As a bonus, you can get rid of your service, so that your app can be better behaved on the user's device.
wait() calls are not guaranteed to wait for the right amount of time if the device goes to sleep. You should use AlarmManager to trigger your countdown timer instead.

When will android stop its cpu without wake lock?

I know android use wake lock to keep cpu running or screen on.
It's obvious that screen wake lock prevents the user active timer from turning off the screen after a period of user inactivity.
But I'm wondering when exactly will the cpu wake lock take effect.
1.If I create a new thread and keep draining cpu in background with out any wake lock, turn off the screen will not stop it. Will it stop and when will it stop?
2.What about a thread scheduled with Timer.schedule()?
3.It leads to another question, if I keep a long socket connection in a service, which is blocked at socket.read(). Do I have to acquire a wake lock to make sure the service will be wakeup when the socket receives any data form remote?
Thanks.
Answers to all your sub-questions
Android sleeps when no wake-lock is active. It does not matter what processes and threads are running it will still sleep. That means if your thread or some other process out there has not activated a wake lock your thread will not execute and hence will not drain any battery. The thread will be made active only when some other process acquires a wakelock.
Same is applicable to the Timer.schedule(). Say you write a Timer that executes every second but without any wake-lock, and say android goes to sleep for 10 seconds. When it wakes p on 11th second it will identify that your timer has expired 10 times it will simply discard the9 instances and execute it only once. If you want very reliable timers you will have to either obtain a wake lock or user AlarmTimer.
Yes.
What i learn from different functionality related to Wake-Lock , Android System will never sleeps, if it sleeps you will not get SMS , Call i.e Android will not run only OS level task when it goes on sleep.
Wake-lock is a mechanism where any application can request system to have the device stay on for him. Application can perform operation on background despite user haven`t move to launch that application.
For 1 & 2 Akshar has explained correctly.
3. As to perform any operations in your application while system is in sleep state(only OS level task are runnning) , you first have to request wake-lock from system and then only application can run its operations. After completing operations you should release the wake lock so that system can move to managing OS level tasks.

Android - periodically wake up from standby mode?

I have an app that needs to send a periodic heart beat to a server, but when the phone goes into standby mode the background heartbeat thread dies. Is there anyway to wake the phone from standby, send the heartbeat and then go back to sleep programmatically? I want to avoid using PARTIAL_WAKE_LOCK if possible.
Thanks
Is there anyway to wake the phone from standby, send the heartbeat and then go back to sleep
programmatically?
Use AlarmManager with a _WAKEUP-style alarm. Here is a sample project illustrating its use (along with a WakefulIntentService you will want, to make sure the device does not fall back asleep during your network I/O).
I want to avoid using PARTIAL_WAKE_LOCK if possible.
You cannot do network I/O without a WakeLock, because the device will fall back asleep during the I/O. Using AlarmManager, you can arrange to only hold a WakeLock during the actual heartbeat work, not 100% of the time.

Categories

Resources