How to Determine If an Android Service Is Running in the Foreground

How to determine if an Android Service is running in the foreground?

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
.next();

if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning = true;

if(runningServiceInfo.foreground)
{
//service run in foreground
}
}
}
return serviceRunning;
}

If you want to know if your service is running in foreground just open some others fat applications and then check if service is still running or just check flag service.foreground.

The correct way to determine if service is running

Create a static boolean in the Service itself.
In onCreate make it true;
In onDestroy() make it false;

public class ActivityService extends Service {

public static boolean IS_ACTIVITY_RUNNING = false;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
IS_ACTIVITY_RUNNING = true;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!IS_ACTIVITY_RUNNING)
stopSelf();
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
IS_ACTIVITY_RUNNING = false;
}}

Now, You can check whether your activiy is running trough boolean ActivityService.IS_ACTIVITY_RUNNING

How to check if a service is running on Android?

I had the same problem not long ago. Since my service was local, I ended up simply using a static field in the service class to toggle state, as described by hackbod here

EDIT (for the record):

Here is the solution proposed by hackbod:

If your client and server code is part of the same .apk and you are
binding to the service with a concrete Intent (one that specifies the
exact service class), then you can simply have your service set a
global variable when it is running that your client can check.

We deliberately don't have an API to check whether a service is
running because, nearly without fail, when you want to do something
like that you end up with race conditions in your code.

How to check in foreground service if the app is running?

There is no proper way to do that. One work around you can use is to start a service normally from your activity and overriding onTaskRemoved method. This method will be called when your app is removed from the recent apps screen. You can do set global static variables in your main service class and access them later on to determine whether the app is killed or not.

This is the service code:

Your foreground service:

Kotlin:

class ForegroundService : Service() {

companion object {
// this can be used to check if the app is running or not
@JvmField var isAppInForeground: Boolean = true
}

...

}

Java:

class ForegroundService extends Service {

public static boolean isAppInForeground = true;

}

Your service for checking app state:

Kotlin:

AppKillService.kt

class AppKillService : Service() {
override fun onBind(p0: Intent?): IBinder? {
return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// service is started, app is in foreground, set global variable
ForegroundService.isAppInForeground = true
return START_NOT_STICKY
}

override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
// app is killed from recent apps screen, do your work here
// you can set global static variable to use it later on
// e.g.
ForegroundService.isAppInForeground = false
}
}

Java:

AppKillService.java

public class AppKillService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// service is started, app is in foreground, set global variable
ForegroundService.isAppInForeground = true;
return START_NOT_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
// app is killed from recent apps screen, do your work here
// you can set global static variable to use it later on
// e.g.
ForegroundService.isAppInForeground = false;
}
}

In your MainActivity:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// start your service like this
startService(Intent(this, AppKillService::class.java))
}
}

How can I tell if Android app is running in the foreground?

Make a global variable like private boolean mIsInForegroundMode; and assign a false value in onPause() and a true value in onResume().

Sample code:

private boolean mIsInForegroundMode;

@Override
protected void onPause() {
super.onPause();
mIsInForegroundMode = false;
}

@Override
protected void onResume() {
super.onResume();
mIsInForegroundMode = true;
}

// Some function.
public boolean isInForeground() {
return mIsInForegroundMode;
}

How to check android service is running in background or foreground using command?

You can use adb shell dumpsys activity services

Foreground services should have isForeground=true in the output



Related Topics



Leave a reply



Submit