I want to check whether my service is running in or not,
I was using below code which was working fine till nougat but not working in OREO
private boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
Log.d(TAG, "isMyServiceRunning: " + service.service.getClassName());
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
But above code is not working for foreground services.
I have referred this but it is also not working..
The ActivityManager.getRunningServices() API has been deprecated since API 26 and is no longer available as of Oreo. It was intended to only be used for debugging, not production apps. You should not rely on this type of API for your app.
Related
As of API level 21, getRunningAppProcesses() returns the app itself. How can I get the processes running in the android ?
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Pass the class that you need to check
I am running a Service to get Notifications every 1 hour in my application.
The service definitely runs every 1 hr; I checked it using the Logcat.
But since I am starting this Service in the onResume() of my MainActivity, I want to check whether the Service is already running, and start it only if it's not running.
I used this code:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.android.MyApp.MyServices.NotificationsService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
But I always get false.
So I checked what output service.service.getClassName() gives. And my Service is not there in the list.
Is it because my application is unknown? or something else?
I know its bit late but this answer might help others who are looking for the same.
Have you started your service by using context.startService()?
Make sure that the manifest file contains following:
<service android:name="com.android.MyApp.MyServices.NotificationsService" />
private boolean isMyServiceRunning(Context mContext) {
ActivityManager manager = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if (**HERE_YOUR_SERVICE'S_CLASS_NAME**.class.getName().equals(
service.service.getClassName())) {
return true;
}
}
return false;
}
// i have background GPS service and i have successfully checked that my GPS service is either running or not.
// Happy coding...
Is there a way to programmatically check if an Android device is running Google Services? I have an application that utilizes C2DM and want to disable menu options including this if the device (such as Kindle Fire and Nook) do not have the Google Services required.
public static boolean doesContainGsfPackage(Context context) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> list = pm.getInstalledPackages(0);
for (PackageInfo pi : list) {
if(pi.packageName.equals(ACCUWX.GSF_PACKAGE)) return true; // ACCUWX.GSF_PACKAGE = com.google.android.gsf
}
return false;
}
I am not sure exactly what "Google Services" is however in the past I have found this function reliable when testing if a service is running. By replacing "some.package.name.MyService" with the Google Services package name then you should be able to check if it is running or not.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("some.package.name.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
I have 3 services running in the background in my application. How do I check if they are running or not.
You can iterate through all services and check if one of them matches name of your service.
private boolean isSomeOfMyServicesRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.domain.myapplication".equals(service.service.getClassName()))
return true;
}
return false;
Use a static field in your service to toggle a boolean flag. Then you can check that flag by binding the service to your activities.
For reasons beyond my app's control, TtsService is stopped by the system, providing only Log.i() hints in LogCat:
05-01 12:01:55.662: INFO/TtsService(1791): Stopping
05-01 12:01:55.662: INFO/TtsService(1791): Stopped
I would like to be able to handle this situation from within my app.
Is there a way (a callback or a system call) to detect when this happens?
If not, is there a way to check whether the TTS service is running?
You can use following method to check whether service is running or not.
ServiceName = "package.service".\\service name
public static boolean isServiceRunning(Context ctx, String serviceName)
{
ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceName.equalsIgnoreCase(service.service.getClassName())) {
Log.v(TAG,serviceName+": Service running");
return true;
}
}
return false;
}