Home » Android » How to rebind to foreground service?

How to rebind to foreground service?

Posted by: admin November 1, 2017 Leave a comment


A foreground service is started and immediately bound to. Is there a way to know whether the serivce has been stopped or is still running after an activity restart. The safe way would be to execute startService anyway, but is there a cleaner one?


You can use this method from:


To check if your service is still running. You can call this method in your onResume() or onCreate(), and if it is not running, I would restart it.

private boolean isServiceRunning(String serviceName){
    boolean serviceRunning = false;
    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
    Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
    while (i.hasNext()) {
        ActivityManager.RunningServiceInfo runningServiceInfo = i

            serviceRunning = true;

                //service run in foreground
    return serviceRunning;