1

Hi I have one activity and one service class from activity am starting service in one point i want to start service but at same point i want to check that service is already running if yes then i dont want to call start service if not then i will strat but my problem is that how i will get to know that service is running or not?

i used below code but it not working for me is any other code which will help me out

  private boolean isServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager
            .getRunningServices(Integer.MAX_VALUE)) {
        if ("TimerService".equals(service.service.getClassName())) {
            Toast.makeText(getApplicationContext(), "running", 100).show();
            return true;

        }
    }
    Toast.makeText(getApplicationContext(), "not running", 100).show();
    return false;
}

this code always show me not tunning even if it is running

4

1 回答 1

1

您无需检查它是否正在运行。两者startService()stopService()都将处理相应的状态,因此如果您调用startService()并且您Service已经在运行,则不会发生任何事情(它将继续运行)。也一样stopService()

于 2014-03-24T19:33:35.130 回答