In my project, I start the service when the button is clicked. But I do not want to start this service again when this button is pressed, if the previous one has not been stopped. Therefore, you first need to check if the service is working or not. I used the following method
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
But it does not work for me, it gives no exception, but always returns false. What should I do now?
source
share