I am trying to convert this code to Kotlin:
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
System.out.println(isMyServiceRunning(ExampleService.class));
Here's what I tried but it's still wrong because I get error Name expected
in the last line:
private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
return true
}
}
return false
}
System.out.println(isMyServiceRunning(ExampleService.class))
Please tell me how to fix it. Original code is from here :https://stackoverflow.com/a/5921190/13976983