1

I am trying to create a MQTT connection/Socket Connection in a background thread in a JobService. My concerns is, while I am creating the connection, the JobService just gets destroyed. Is it a way that it stays alive for sometime in background ? OR it will always get destroyed ?

Navratan Soni
  • 179
  • 1
  • 8

2 Answers2

2

Use JobService.jobFinshed. Explanation here: Android: How to use JobFinished of JobService

But in short: spawn a new thread when you wake up in onStartJob and return true value from the method (this tells the OS to not kill you and keep a wake lock for you). Then, after done working on separate thread, call JobService.jobFinished (from the bg thread you spawned) to finally tell OS to release wake lock and kill the scope/context.

Again, code example here: Android: How to use JobFinished of JobService

Arseny Levin
  • 664
  • 4
  • 10
0

How long a JobService stays alive?

Until the app is alive or you yourself kill the app.

In android O, even background services are automatically destroyed/killed by the OS as soon as the Application is swiped-off/killed.

Daksh Gargas
  • 3,498
  • 2
  • 23
  • 37