I have a test case in which i am starting a service by clicking a button. After this i want to verify if it has started or not. how can i do this?
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
String serv = service.service.getClassName();
}
return false;
}
i used this in my test cases but getSystemServices() is not recognized?
Where do you run this code from? Either way you need a Context in order to use getSystemService()
Context context = ...; // get your context
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
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 have an app that gets GPS co-ordinates. It works fine but i want to test from within the calling activity if the service is running. I have the following code that i thought would check but it is returning false when the GPS icon in the notification bar is still flashing. Has anyone any ideas why? Thanks in advance
private boolean isGpsServiceRunning() {
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("LocationService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
.
LocalBroadcastManager.getInstance(getApplicationContext())
.registerReceiver(
locationChangereceiver,
new IntentFilter(
LocationChangeIntent.ACTION_LOCATION_CHANGE));
startService(new Intent(this, LocationService.class));
You should compare against LocationService.class.getName(), instead of using a hard coded value. Also, the value that you are using corresponds to the Class#getSimpleName() which is probably not what you want here.
public boolean check(){
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
{
if ("com.example.yourpackagename.YourserviceName"
.equals(service.service.getClassName()))
{
return true;
}
}
return false;
}
this method works fine for me and is easy to use:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
Log.i(TAG,"MyService.class.getName() = "+ButtonOnBootService.class.getName());
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
Log.i(TAG,"service.getClassName() = " +service.service.getClassName());
if (ButtonOnBootService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
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;
}
I found some code that tells you if your service is running. But it is getting compile errors and I don't know why. My service class name is AudioService (the class that extends Service) and I am getting compile error on the if statement line of code where it says,
AudioService.equals(service.service.getClassName()) and the error message from the compiler is,
Cannot make a static reference to the non-static method equals(object) from type object
I am using the method inside of the Activity that is bound to the service. I call bindService() in the onResume method and unbindService() in the onPause. Just want to use some method to make a Toast message to show that the service is actually running.
What am I doing wrong and how to get this code working?
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (AudioService.equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Use this AudioService.class.getName()
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (AudioService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Try this
public boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if ("com.application.MyService"
.equals(service.service.getClassName())) {
return true;
}
}
return false;
}
How do we know if a service has stopped in a bound and unbound service?
I want to check constantly if a service has stopped and what is its state/status?
Thanks
Sneha
try this :
public boolean isServiceRunning(String serviceName) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> infos = manager.getRunningServices(30);
for (RunningServiceInfo info : infos) {
if (info.service.getClassName().equals(serviceName)) {
return true;
}
}
return false;
}