How and where is better to execute a periodic code on Android - android

Hi everyone (i'm a newbie in android and kotlin),
What i need to do is send periodically (every 10 sec) some information, of may application, on a remote server whit a POST request independently if the application is in foreground or background.
Now i found some interesting source like this: https://guides.codepath.com/android/Repeatinag-Periodic-Tasks#alarmmanager.
But i have some perplexity:
how would you structure this task?
is a bad idea to start aThreadPool on an application class?
is better to use a Thread or service?
Thanks everyone in advance!
More Info:
The information in question are logs, so every 10 sec the app send it's logs(if present) to a logstash -> Elasticsearch.
I know that 10 sec is a lot, and it will drain the battery of the device, but i'm not the one in charge of this decision and i can't change it.

Related

How to impelement notification system in android?

I am building kind of an android chat app and I want to ask how I could build up a background thread that checks for incoming messages (from a rest api). My current solution is a JobService but I expect it to have a high battery consumption. Also the JobService has another problem: After closing the app it takes about 3 minutes to get started again (I want it all the time:)
I prefer to not use firebase btw
Thank you very much in advance.

Background accumulated tasks executed periodically

I am devolping an Android App that need to execute an accumulation of internet tasks periodically (like a web bot). Those tasks need to get stored at a specific time so I thought about using Alarm Manager and a
embeded Database. Due to it, the app could be active much more time, although those save tasks do not need web connection. Later I will throw another Alarm Manager to execute all the tasks queued and do web stuff.
Otherwise I am not sure if it is better to use a foreground service. The app will be working all the day saving the tasks (each 5 or 15 min) but only running task queue with internet each 30mins.
I feel capable of developing both systems but I would like to know which one is better in terms of performance, of battery consumption.
Thank you very much.
Only a recommendation try to find and use a nice library to do it, every Android Update change something about foreground/background servicess.
one is
TimedDog
and for more
enter link description here

Repeat a function every one minute in background with flutter

I'm writing an app with flutter to communicate with a weather station. The smartphone/tablet have to send via usb a command every one minute, recieve data from the station, and then send this data in a Firebase database.
I know that a smartphone is not the best device for do this, but I'm using an old device and I don't care about battery health (maybe a raspberry could be a better solution).
I'm using usb_serial package to communicate via usb and cron package for timing the events. And all works fine.
The problem is that the app have always to run in foreground. The first solution that i tried is using two apps: (1) one that keeps the screen on (2) and another that makes the screen black to reduce the energy consumpion. This is not the best solution.
I think that a better way is to use a background execution. I made some attempts with workmanager package but the minimum frequency is 15 min, with android alarm manager package but I'm not sure that wakes up also the network connection.
Anybody can suggest me witch is the best way to achieve my goal?
Thanks.
Refer this answer https://stackoverflow.com/a/14946013/13892187
In Dart for setting a recurring function we use the Timer class

Periodic background check for a very simple task (Android)

I'm currently making an app in Android that is checking an API which returns two things. Some text and a colour.
However I want this to be checked for updates every 15 minutes in the background and check every 5 seconds when the app is open. When running in the background it should give a notification if the status is changed.
Now I have checked numerous stackoverflow q&a's and forums, docs etc.. But I can't seem to find a good baseline for what I need. So many documentation that contradicts eachother.. I think that I need an Alarm Manager or a Service... but what do you guys suggest for my problem? The app may not harm the battery too much.
What I really would like to have is that the application doesn't have to "poll" the server every 15 minutes but that the application gets interrupted like.. "hey, there is a new status update". I can't imagine that messaging apps are constantly polling a server for updates? I haven't found much information about that topic... Any help is appreciated. Not asking for code but directions to get where I want to go.
Many thanks
If you're looking to poll the server every X seconds/minutes, AlarmManager(android guide, tutorial) is exactly what you need. However, as you point out this is probably not the best way to go about things. While the app is open you may want to look in to passing messages between the device and server via an open Web Socket. Once your app is closed you could, instead of the app polling the server, have the server push a notification, via GCM or some such, to the app when an update is available.
If you are doing both the server side project and the mobile application, You can use Any messaging service rather than polling for the server, Because there has to be a pusher implementation from the server side to push the status to the MS.
For now GMS is free, I hope it will remain the same :). Otherwise, You can use AlarmManager and IntentService to achieve your goal.

Best Way to Frequently Poll a Server from an Android App

I have an Android app which needs to poll a web server at 2 second intervals.
So I would like to know what the best way to do this would be. My first thought was to use an AlarmManager but I believe this is no good for anything more frequent than about 5 minute intervals. I have also considered using a service but I am concerned this will drain the battery. Are there any options I haven't considered? What is the best way to poll a server very frequently without killing the battery?
I also know that GCM is the ideal way to sync with a server but unfortunately it is not an option at this time.
Edit: ok, it seems from your comments that it is as I feared and there's no good solution for this. I will probably implement it in a service and press for a push mechanism instead. Thanks for your help.
The solution I have come to is gaining agreement to redesign so that the server pushes to the app only when the data has changed (it won't change that often, realistically).
In the meantime I have made some small changes to the service so that it cancels all requests when the app isn't in the foreground and reduced the poll time to 5 seconds (better than nothing, right).

Categories

Resources