2

I have a sensor which connects with my app via bluetooth. I have created a Foreground service which keeps the app active and connected with the sensor even after user close the app. To stop the Foreground service, user relaunch the app and stops the foreground service.

All the above is done perfectly and works fine for NOW.

My problems are -

There is a play and pause button to start and stop the service. Once started, the icons changes but relaunching the app after closing the app completely (without stopping the foreground service), app doesn't change. Of course it doesn't change because I have no idea how to check if service is still running in the foreground or not.

Yes, I have read all the previous answers and most of them are either deprecated or doesn't work anymore.

Since Commonsware is asking for the link for all the posts I have read so far, here you go -

  1. Determining the current foreground application from a background task or service
  2. How to check is app in foreground from service?
  3. check android application is in foreground or not?
  4. Check if Foreground service is running in Android OREO
  5. How to determine if an Android Service is running in the foreground?
B L Λ C K
  • 600
  • 1
  • 8
  • 24
  • "I have read all the previous answers" -- links, please. "most of them are either deprecated" -- links, please. "or doesn't work anymore" -- links, please. Assuming that your service is in a separate process (`android:process` attribute), you should be able to try binding to it without `BIND_AUTO_CREATE` and see if the bind succeeds. Or, use broadcasts (carefully). If you have tried things, perhaps a [mcve] would help us find the problems in your implementation. – CommonsWare Apr 13 '20 at 21:20
  • 1
    okay -- I will add them in my original post – B L Λ C K Apr 13 '20 at 21:38
  • 1
    @CommonsWare Thanks for the hint. – B L Λ C K Apr 13 '20 at 21:46
  • Why to check if the service is running ?? you can easily save a shared preference store the state of your service "on" or "off" , and check it in the on resume event to change the image of button – Mohammed Alloboany Apr 13 '20 at 22:03
  • 1
    @MohammedAlloboany what if user completely close the app? The app will continue to run in the background. – B L Λ C K Apr 13 '20 at 22:30

2 Answers2

1

Try this Util to check if service is running:

    public static boolean isServiceRunning(Context context, Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
Aman Arora
  • 134
  • 7
1

I found a really dirty workaround to check it, but it works. I would prefer to leave with the problem of inconsistency on UI and I didn't use it in my case, because I have switch instead of buttons, and switch can be easily changed by the user.

In my case, the foreground service is running alongside with notification, so there is a possibility to check notification status rather than foreground service status.

var isServiceRunning = false;

val manager = getSystemService(NotificationManager::class.java)
for (notification in manager.activeNotifications){
    if (notification.notification.channelId == "ServiceChannel") {
        isServiceRunning = true;
        break;
    }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
yarsanich
  • 174
  • 1
  • 1
  • 10