Android - periodically wake up from standby mode? - android

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.

Related

Android prevent sleeping

Our company is developing an android application that uses network communication to send GPS signals from devices. The devices are the same and they are all work tools, so we do not have to worry about battery draining, or etc. Currently the activity has a thread, which communicates with the server. The problem is that when the device is locked and it goes to sleep, the network communication breaks.
I've tried to put a partial wake lock to the onPause method to keep the CPU on, and release the wakelock in the onResume method, but it seems not to work. Any idea how to prevent the sleep, or keep flowless communication between the client and the server?
Unfortunately, it is the new behavior, You can read here:
https://developer.android.com/about/versions/nougat/android-7.0-changes.html
See the Doze section.
When a device is on battery power, and the screen has been off for a certain time, the device enters Doze and applies the first subset of restrictions: It shuts off app network access, and defers jobs and syncs. If the device is stationary for a certain time after entering Doze, the system applies the rest of the Doze restrictions to PowerManager.WakeLock, AlarmManager alarms, GPS, and Wi-Fi scans.
It can be solved by using Foreground Service like mentioned above in comments by #egoldx.
It is surely a bad practice to try holding a wakelock, even partial, all the time.

Acquire wake lock, release it and acquire it again while the phone is sleeping

I think this is pretty much the standard case already described in other SO question but I still need a clarification on this matter:
So I have an Android app with an Actvity and a Service. The Activity is not of interest but the Service. The Service has to send some message to a remote server every minute. From what I understand, I need to use WakeLocks to keep the CPU running while allowing the screen to go off (so that I can fix the problem where the service stops when the screen is powered off). So far so good.
My question is: can I acquire the lock, send the message to the server, release the lock AND acquire it again after one minute so that during this one minute pause the CPU is sleeping, too. With the ultimate goal to save the battery. I fear the answer is "no" because once you let the CPU to sleep, you cannot wake it up unless from a lower level (OS and not app).
Best regards
The response is simple: no. What you can do in this case is set a PendingIntent and use the Android Alarm manager to be woken up every minute.
The alarm manager is the way to go - but you also need to delegate from the alarm receiver to a WakefulIntentService to do the work (as the receiver will ANR after 5 seconds). See PowerManager.PARTIAL_WAKE_LOCK android for links.

What is influenced by Android Wakelock?

I am currently developing an app that runs a service from time to time. Currently, the service acquires a wakelock, reads some sensors and sends some information over WIFI (if any). Now what I want to know is weather a wakelock influences sensors and connectivity or not. Is it possible to do these tasks without any wakelock?
Cheers
A wake lock is essentially used to lock the device in an "awake" state, in which the CPU will be on, and the screen may or may not be on.
It is not possible to do these tasks without a wakelock if the phone is in sleep otherwise, as then the CPU is also in sleep mode. However, if the user is using the device for something else, and your app is in the background, you can do these tasks without a wakelock.
Keep in mind that almost everything you're doing is battery intensive (sensors, WiFi, wakelock) and you should not do it too often so that you don't degrade the user's battery life.

Allowing the phone to sleep while using RTC alarm on Android

I've been seeing some strange issues using the Alarm manager in Android, despite the fact that I'm using RTC (non Wakeup) the phone will reliably send the PendingIntents on the correct repeating intervals.
Details of my test
Device is not charging, just sitting on my nightstand while I slept
My service woke up on its repeat interval (30 minutes, an extreme I know) EVERY TIME
The service logged its activity in a file so I could read it in the morning
Now from my understanding the phone should be sleeping unless I wake it up and my Alarms should not be sent until the phone is awake.
Why was my service executing?
If another service is misbehaving and using the _WAKEUP variants of the alarm will my service wake up too?
Can I avoid being woken by another service, and just awake from the user turning the screen on?
Why was my service executing?
Presumably something else was having the device awake at those moments.
If another service is misbehaving and using the _WAKEUP variants of the alarm will my service wake up too?
Yes, though "misbehaving" is in the eye of the beholder.
Can I avoid being woken by another service, and just awake from the user turning the screen on?
Not directly via AlarmManager. You can watch for ACTION_SCREEN_OFF and ACTION_USER_PRESENT broadcasts, and perhaps disable your alarms between those.
I've just spent an hour trying to find out why my RTC alarm sends PendingIntents even when my phone is sleeping. And the answers is very simple, because it was pluged with USB so the phone had status "charging".
Presumably something else was having the device awake at those
moments.
A lot of applications with notification ads (like AirPush, Leadbolt ect) wake up the device.

Network access when the Android phone is asleep

I'm using a combination of alarm (set with AlarmManager) and background service to periodically synchronize data in my application.
The only problem I have is that when sleep policy terminates Wi-Fi connection the synchronization no longer works.
Is there a way to "wake up" the Wi-Fi connection that has been put to sleep? GMail somehow manages to do that because it notifies me about new e-mail even if the phone entered sleep mode.
[update]You can use a WifiLock to keep WiFi active, while holding the lock.
Using an AlarmManager and a Service says to me your service is running only for a very short time!? I think (though can't say for certain) that you should make your service a forground service (check the blog for a good way to implement this on both 1.x and 2.x+) and leave the AlarmManager out of it.

Categories

Resources