0

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.

SoncioZ
  • 3
  • 4
  • 2
    This kind of thing should be done with [WorkManager](https://developer.android.com/topic/libraries/architecture/workmanager) although if you need to do something every ten seconds, I think that is too frequent for it to handle and you will need to use a foreground service. Doing something every 10 seconds will prevent the device from sleeping, so your app will be responsible for draining the battery. – Tenfour04 Jul 09 '21 at 18:43
  • I would seriously consider using `Flow.sample` with a sample interval every ten seconds on an appropriate dispatcher. That would also prevent sending updates if the value doesn't change -- which might address that sleeping issue. – Louis Wasserman Jul 09 '21 at 19:49
  • Consider that 1 POST request per 10 seconds is a lot. You probably want to make use of websockets to minimalize the impact on your backend. If your app gets popular, your backend might otherwise not survive it. Additionally, it might be good to pool multiple requests together (if the business logic allows for it) and only send once every minute or several minutes. – ByteWelder Jul 10 '21 at 11:34

0 Answers0