Check if service is running without using getRunningServices


Check if service is running without using getRunningServices



I have an app:
The app starts,the user presses a button,a service starts, the user can destroy the activity, but keep the service running.
If the user restarts the activity, how can i check that the service is running to display an appropriate message?
this
How to check if a service is running on Android?


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;
}



uses deprecated methods. I was thinking of using a broadcast receiver in the activity and another one in the service to communicate, I was wondering if there was a better, more elegant way, and if not, what the timeout should be (how long does it take for a broadcast to be received)?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages