Network access when the Android phone is asleep - android

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.

Related

Sleep/idle mode leads to Wi-Fi lost

When I have internet connection, if I leave my Android wear device idle for around 20 to 30 minutes, internet connection stops.
In my app, I have connected with third party server, so sleep/idle mode leads to lose the connection with the server.
How can I handle this situation?
My requirement: my app should be always connected with the server to receive notifications.
If you want real time notifications, you'll need a Partial Wakelock to keep the service running, plus a WiFiLock to prevent the wifi network from going down. If you don't need realtime notifications, you can use AlarmManager or BroadcastReceiver or WakefulBroadcastReceiver or JobScheduler to schedule checks for new notifications, say every hour or so.
Note that if you use BroadcastReceiver, it is not guaranteed that CPU will stay awake when the broadcast is received inside onReceive(). However, if you use WakefulBroadcastReceiver,the CPU is guaranteed to stay awake until completeWakefulIntent is fired. WakefulBroadcastReceiver has been deprecated in Android O, in favour of JobScheduler
You can also read here about scheduling repeating events.
For devices with Doze mode:
The system exits Doze for a brief time to let apps complete their
deferred activities. During this maintenance window, the system runs
all pending syncs, jobs, and alarms, and lets apps access the network
If you need to set alarms that fire while in Doze, use
setAndAllowWhileIdle() or setExactAndAllowWhileIdle(). Alarms set
with setAlarmClock() continue to fire normally — the system exits
Doze shortly before those alarms fire.
Refer Optimizing for Doze and App Standby

Using internet during sleep - Android Service

I am currently developing an android app that gets data from a web server at a user specified interval (5 min, 10 min...). I am using an AlarmManager and a WakeLock. The alarm goes off as expected every 5-10 minutes. The internet connection though doesn't seem to be working during sleep. Most people suggest that I use a WifiLock. Correct me if I am wrong but isn't WifiLock only used to keep WiFi alive? What about 3G-4G mobile data? Does WifiLock keep that connection alive aswell?
Starting with Android 6 due to the new Doze Mode the device enters sleep even with wakelocks, they are ignored.
The way to avoid the device to enter sleep is to start a foreground Service with a non-dismissable notification.
You can't get network access while the device is in Doze mode. If you are hoping to receive notifications or updates from a web service, use Firebase Cloud Messaging, or just defer the network call until the next time it wakes up. You can see more information about Doze restrictions here:
https://developer.android.com/training/monitoring-device-state/doze-standby.html#restrictions
Also, if you are curious about how to check if phone is in Doze mode:
https://developer.android.com/reference/android/os/PowerManager.html#isDeviceIdleMode()

Android device is sleeping when locked

I m actually developping an application in which I need my phone to be active even if it's locked.
Explanations
The applications connect to a remote nodejs server using socket.io. In that case, it cans sends realtime messages to a socket server that can handle this and make anything it needs. The fact is the socket management is in a service class (extends Service class).
The problem is that, when I lock the phone, the device stop to send heartbeat, and so is disconnected (by timeout) from the nodejs socket io server.
Actually, the normal behaviour of an android phone locked is to sleep. Meaning no activity.
Question
I was wondering how does others applications to be able to receive notifications, and so handling notif by a background service. it means that there's an activity even if the phone is sleeping right ?
How can I do to make this without draining a lot of battery ?
You use wake locks to keep CPU awake while locked: https://developer.android.com/training/scheduling/wakelock.html
You should aquire it for a small amount of time to not drain battery.
I think if you will acquire a phone wake lock it will drain the battery which is not a good thing . On the other hand, The use of service is really very good,and you talk about the notification of other apps and they works in background because notification does this by default
So in you case As I do not know what are you really trying to do with server, But Service is good option. Service works even your mobile is locked. But in your case if it stops make sure it is not bind with the class.
You should make sure that the Service is not binded with the class or activity as when the activity will be destroyed the service would also be. Just trigger your service and let it handle all the things in background.Please read the discussion in this link. It might help you in understanding better.

AlarmManager vs Service for db and FTP connection. Which and how to use?

My question is rather simple but might come with a complex answer.
I'm making an App that checks on an online mysql db (via a php script on the website) for new updates. Sometimes this updates will tell the App it has to download form a FTP server.
The App should start on boot and check for updates every 15 minutes.
I've read in the web I should either use a service or a AlarmManager but I don't know which one is better.
Also, I've read a lot of pages that say that AlarmManager will "Wake Up" the device but I've failed to understand what this really means and why it's different in a service. Does this means that if the Phone is turned off it will turn it on or that it will turn on the screen?
I only need the phone to do the task in the background when it's on, I don't need it to turn the screen on or power up the device.
I've read in the web I should either use a service or a AlarmManager but I don't know which one is better.
It's not an "or". It's an "and". You will need to use AlarmManager to trigger the work to be done by a Service.
I've read a lot of pages that say that AlarmManager will "Wake Up" the device but I've failed to understand what this really means
An AlarmManager _WAKEUP event type (e.g., ELAPSED_REALTIME_WAKEUP) will wake up the device out of sleep mode. That, in conjunction with something like WakefulBroadcastReceiver and an IntentService, can arrange for you to do your work periodically even though the device would ordinarily be asleep (screen and CPU in a sleep state).
I only need the phone to do the task in the background when it's on, I don't need it to turn the screen on or power up the device.
Then you can use AlarmManager with a non-_WAKEUP alarm type (e.g., ELAPSED_REALTIME). I would still recommend using WakefulBroadcastReceiver and an IntentService, to make sure that the device does not fall asleep in the middle of what you are doing, as that may cause problems for your work.

Android: Does internet (mobile data connection) switch off when mobile goes to sleep?

I am running a remote service which has a handler which does an operation every 15 minutes (handler.postDelayed()) which involves making an internet request. Sometimes i notice that it does not happen. Does the internet (mobile data connection get switched off when the mobile sleeps or processor sleeps). What happens during this time ?
I guess internet gets turned off when the screen goes to sleep. What could be the efficient way to make sure that in the required time interval(half an hour) it wakes up (turns the internet on) and does the job in the remote service? This service is supposed to run in the background throughout. Can i do this with an AlarmManager (can it trigger some job to happen in a remote service)?
As stated in a related question's answer, you can give a try to WifiLock if you use only Wi-Fi for this service.
Another reason is maybe your service is destroyed by Android when it sees no use. If it is the case you can try the Foreground service option. Note this does not guarantee the service won't be destroyed but the priority compared to other service will be decreased, thus giving more chance to your service to survive.

Categories

Resources