I want to define a flag in android sharedPreferences to check whether a service is running or not, but it can't be at runtime, so is it possible to do it somehow in xml? also, is there anyway that i can check whether a service is running or not?
Asked
Active
Viewed 104 times
1 Answers
0
I don't get why you can't define a flag for SharedPreferences, but if you want to check if service running you can create global variable and check it:
public class MyService extends Service {
public static boolean sIsRunning;
@Override
public void onCreate() {
super.onCreate();
sIsRunning = true;
}
@Override
public void onDestroy() {
super.onDestroy();
sIsRunning = false;
}
}
Or check running services with ActivityManager:
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

Community
- 1
- 1

Bracadabra
- 3,609
- 3
- 26
- 46