I have a service which I believe to have running in the foreground, How do I check if my implementation is working?
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
.
public static boolean isServiceRunningInForeground(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())) {
if (service.foreground) {
return true;
}
}
}
return false;
}
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
.
A more efficient variation of answer: https://mcmap.net/q/83152/-how-to-determine-if-an-android-service-is-running-in-the-foreground
public static boolean isServiceRunningInForeground(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 service.foreground;
}
}
return false;
}
As of API 26, getRunningService()
is deprecated.
One solution now is to bind your Activity to your Service. Then you can call a method of your Service from your Activity, to check if it is running.
1 - In your Service, create a class that extends Binder and returns your Service
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
2 - In your Service, declare the binder
private final IBinder binder = new LocalBinder();
3 - In your Service, implement onBind()
, which will return the Binder
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
4 - In your Service, create a method that check if it is running (for example check if variables are initialized)
public boolean isRunning() {
// If running
return true;
// If not running
return false;
}
5 - In your Activity, create a variable that holds your Service
private MyService myService;
6 - Now, in your Activity, you can bind to your Service
private void checkIfEnabled() {
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
MyService.LocalBinder binder = (MyService.LocalBinder) service;
myService = binder.getService();
// Calling your service public method
if(myService.isRunning()) {
// Your service is enabled
} else {
// Your service is disabled
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
// Bind to MyService
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
For more info, check Bound services overview from official documentation.
Starting with API29 (Q), you can simply check it with the following code.
Can be called from inside your service. If you don't have your service instance, you can obtain it by using binder.
class MyService : Service {
val isForeground: Boolean get() = foregroundServiceType != FOREGROUND_SERVICE_TYPE_NONE
}
FOREGROUND_SERVICE_TYPE_NONE
only until it has been started once. Calling stopForeground()
, whether or not you remove the notification, will still result in foregroundServiceType
not being FOREGROUND_SERVICE_TYPE_NONE
. I would recommend incorporating one of the getRunningService()
answers (and check the foreground
field), even though it's deprecated. –
Leodora Little touch on Adam's answer:
@Suppress("DEPRECATION") // Deprecated for third party Services.
fun <T> Context.isServiceForegrounded(service: Class<T>) =
(getSystemService(ACTIVITY_SERVICE) as? ActivityManager)
?.getRunningServices(Integer.MAX_VALUE)
?.find { it.service.className == service.name }
?.foreground == true
This worked for me in the Coinverse app for crypto news.
It's is the most concise Kotlin solution. Thanks to Abbas Naqdi in this GitHub issue.
@Suppress("DEPRECATION") // Deprecated for third party Services.
fun <T> Context.isServiceRunning(service: Class<T>) =
(getSystemService(ACTIVITY_SERVICE) as ActivityManager)
.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == service.name }
it.foreground
. –
Edsel © 2022 - 2024 — McMap. All rights reserved.