0

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

Bandy
  • 161
  • 12
  • try doing ExampleService::javaClass – Plee Jul 31 '20 at 23:56
  • @Plee now i get `type mismatch` – Bandy Jul 31 '20 at 23:59
  • Can you please add a comment indicating where the error is. Can you also post the full error? – Plee Aug 01 '20 at 00:01
  • 2
    Use `ExampleService::class.java`. You can also replace the last six lines of your function with `return manager.getRunningServices(Int.MAX_VALUE).any { it.service.className == serviceClass.name }` – Tenfour04 Aug 01 '20 at 00:07
  • @Tenfour04 ok. now I get this weird error: Circular dependency between the following tasks: :mylib:generateDebugRFile \--- :mylib:generateDebugRFile (*) (*) - details omitted (listed previously) – Bandy Aug 01 '20 at 00:42
  • 2
    I don't recognize those names and they seem to have nothing to do with the code above. – Tenfour04 Aug 01 '20 at 01:31

0 Answers0