Правильный способ определить, запущена ли службаAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Правильный способ определить, запущена ли служба

Сообщение Anonymous »


I am running foreground services in my app and I need to deremine if they running in my activity. From my searches I found that this is the option:

private fun isMyServiceRunning(serviceClass: Class): Boolean { val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager for (service in manager.getRunningServices(Int.MAX_VALUE)) { if (serviceClass.name == service.service.className) { return true } } return false } but:

getRunningServices(Int): is deprecated.

So I am using one of 3 ways.
  • Binding the service to the activity with onResume. But I think it a bit overkill for something small just to check if service is running.
  • Make the Intent public and check if its null, but there can be some cases where the intent not null but service not running
  • Check if the foreground service persistent notification is active, but this is workaround.

What is the most correct way to check if service is running?


Источник: https://stackoverflow.com/questions/667 ... is-running
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»