Android check for running services in application - android

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.

Related

Check if Foreground service is running in Android OREO

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.

Service getting called multiple times?

I am developing an application where I am running some background operations and displaying notification to the user when certain condition meets .When the user clicks on the notification it should take him to the main activity , with out restarting the service .
My problem is when I am clicking the notification it takes me to the main activity and starting my service again.I dont want to restart my service again .How can I achieve this .
I have used the following code to check weather a service running or not lease look at it
public static boolean isServiceRunning(String serviceName,Context context){
boolean serviceRunning = false;
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
while (i.hasNext()) {
ActivityManager.RunningServiceInfo runningServiceInfo = (ActivityManager.RunningServiceInfo) i
.next();
if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning = true;
}
}
return serviceRunning;
}
I have used this piece of code and it's working fine :)
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
{
if (MyService.class.getName().equals(service.service.getClassName()))
{
found = true;
}
}
you could check getIntent() on the activity and put some extra on the intent bundle of the notification that you check to start or not the service.
Note:
if a service is already running, it is not restarted.
Finally I have rectified my code.as follows
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.sevicedemosolution.Myservice".equals(service.service.getClassName().toString())) {
return true;
}
}
return false;
}

Android: check if my Service is running in the background

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...

How can I detect if my Android app is displayed?

I have an Android app with a service that runs in the background doing various client/server connections. How can I check in my running service if any screen from my app is displayed?
You can get the list of running processes and check if yours is in foreground with the following code:
ActivityManager actMngr = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses = actMngr.getRunningAppProcesses();
for (RunningAppProcessInfo pi : runningAppProcesses) {
if (getPackageName().equals(pi.processName)) {
boolean inForeground = pi.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
}
}
Check more at http://developer.android.com/reference/android/app/ActivityManager.RunningAppProcessInfo.html
Refreshing an Activity from service when Active
a few options:
•Have the activity register a listener object with the service in onResume() and remove it in onPause()
•Send a private broadcast, picked up by the activity via registerReceiver() when it is in the foreground
•Have the activity supply a "pending result"-style PendingIntent to the service
•Use a ResultReceiver
•Use a ContentProvider, with the activity holding onto a Cursor from the provider, and the service updating the provider
see here https://github.com/commonsguy/cw-advandroid/tree/master/AdvServices/ for a few examples.
To check whether a service is running use
ActivityManager manager = (ActivityManager) MyApp.getSharedApplication().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (CTConstants.MyService_Name.equals(service.service.getClassName())) {
//Your service is running
}
}
if you don't want to generalize this as a common function then replace the first line with
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

How to check whether TtsService (or an Android service) is stopped?

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

Categories

Resources