Android: check if my Service is running in the background - android

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

Related

Controlling background service from another instance of an app

I made an application that fires a long-lived background service. Sometimes the application itself is closed, and the service keeps running, which is the way I want it.
The problem is that I don't know how to control that service after the caller application is closed. For example if the users re-opened the app, I'd like to give them the option whether to kill that process or keep it running.
I am able to detect if the service is running by using the following function (taken from this solution):
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;
}
I tried then calling this line from the activity, but that didn't do anything:
if(isMyServiceRunning(MyService.class)) {
stopService(new Intent(this, MyService.class));
}

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

When an android service is really started?

I have tested this method:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
When I call a bindService() in my activity without startService(), my service is running (checked it with the prevoious method) but I was thinking that only startService() can start a service!
So I just want to know when a service has really begun: with startService() or binService()
Just a comment, Only when I call unBindService(), the previous method return false.
Thanks.
getRunningServices() returns the running services. Services will be running if they have been started by startService() or bound to via bindService().

Android check for running services in application

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.

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