Schedule task when other app is using network - android

In my app I show an persistent notification on status bar in which real time data is shown. However I cannot just make a poller for the real time data because it will cost too much battery. What I am thinking is that I update the data whenever some other app is using the network. So that network requests are batched and battery consumption will be reduced. Is there an api in Android to do such scheduling? Thanks!

Yes, you can achieve scheduling tasks by using the AlarmManager class, you can have look at the offical doc.

Related

Invoke a custom code on internet connectivity in Android

I want to get latest updates from my server when I turn on the internet and generate a notification, but when the app is closed/killed/swiped from recent items, there is no way to keep my service alive and listening to network change event so that I can ping my server, I'm not sure how to do it and how other apps such as whatsapp does it when we receive new notification the moment we turn on mobile internet.
Any help is appreciated, thanks in advance.
Making clear what #GobuCSG commented. You have the following options:
Run your application all the time via a foreground service and listen for wifi connected broadcast. This is not a good solution and wastes battery life.
Schedule jobs using the WorkManager API.
The WorkManager API was developed specifically for the purpose you specified. The API allows you to set required conditions for your job to run, such as network connectivity. It also persists scheduled jobs through device reboots, so you don't have to. The only downside is that you don't have fine tune control of when the code runs, as one of the goals of WorkManager is to save battery life by delaying and batching jobs.
EDIT:
Another option is to use push notifications. This is useful if your are developing a messaging app and you want your server to push a message to the client so they can be notified of a received message. But, if all you need to do is establish contact with your server once a day, then you should use WorkManager.

Android - reliably ping server every few seconds

I have a business requirement that the Android app needs to report to the server every few seconds (all the time, 24/7) on a dedicated device.
First I thought that it can be done with the PeriodicWorkRequest, but I've read its minimum interval is 15mins.
What would be the best way to achieve this? What mechanism I can rely on to be sure that the process won't be killed?
Is it possible to do with WorkManager? Should I have a foreground service with a loop?
I think you could schedule it the other way. Make your server send a notification to your android device,could be done easily via firebase. Then handle notification in your app and report back to the server.

Android migrate background service to jobscheduler

we have a project which uses background service to retrieve real-time events from server. we use s websocket connection to retrieve data. but now in Android Oreo or higher, os starts to showing battery Warning.
my concern is that if we migrate to jobscheduler does it stop after sometime. we want to show a notification if event happen
i tried job scheduler and it offers recurring invokes. but our connection is real-time so if jobscheduler stops we don't get data.
so how other apps handle this (example WhatsApp )
what is the best way to keep our connection live and retrieve data ?
thanks advance
my concern is that if we migrate to jobscheduler does it stop after
sometime.
Yes. During doze mode all the scheduled jobs are deferred until maintenance window is made available by the OS.
what is the best way to keep our connection live and retrieve data ?
You shouldn't maintain a persistent connection with your server and permanently run background service. Starting from Android O you won't be able to run Services in background. OS will terminate all the services and any attempts to start service in background will result in IllegalStateException
Your requirement seems that you need to use FCM. The basic idea is, instead of constantly asking server for data, let server notify the app about data availability. For more details and implementation steps, refer to official document.

Best Practice for network operation at regular intervals?

I am making an application which contains performing network calls to look for updates. Can anyone help with some best practices to perform this task. I can make a service to run on background and perform network operation using Handler at regular intervals but this would consume a lot of data and battery. Is there any other way to do this?
Use an AlarmManager or JobScheduler (depending on API level), and pick a sane frequency. Doze mode will stop you from going too insane.
You could work with Push Notifications from any server when the backend recognizes an update. It's better for the server itself to listen to updates at a regular interval of time rather than the app, because of the reasons you listed.
When the app receives a push notification, it would mean that it needs updating.
There are a couple of options for you:
Azure Notifications Hub: https://learn.microsoft.com/en-us/azure/notification-hubs/
Firebase Cloud Messaging: https://firebase.google.com/docs/cloud-messaging/

Syncing the website account with android app

i was thinking if there is anyway to sync android app with a web/website account. Basically, if the user logs in through the web, i want the android app to log in/start up/show notification. Should i use a background service in android to monitor the account or something else
Please help
Thanks
As far as I can see, you have a couple of options:
Use a background service (as you suggested) which is constantly running and checking. This will be battery consuming.
Use the AlarmManager to schedule a repeating alarm to check on a scheduled interval. This will be less battery consuming but also less immediate (unless you set a low interval which will consume a lot of battery).
Use a push message. This will be the most accurate and least battery consuming for your phone, but it's also the most advanced (difficult) to set up.
Based on your requirement, optimized solution is to use GCM - Push Notification. Based on the whatever action occurs at web, you just need to send a push notification to clients. Based on the push notification IDs, client needs to perform appropriate action.

Categories

Resources