compile error on method to check if service is running - android

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

Related

OnCreate in Application class called twice

I didn't use android:process in my AndroidManifest. When I launch app oncreate in Application class get called twice. I would like to know what could cause such behaviour.
Probably you call your service when you run your app.
Use this to prevent recalling the service several times:
Intent intent = new Intent(YourActivity.this, YourService.class);
if (!IsServiceRunning(YourService.class)) {
startService(intent);
}
Use this function to check whether the service is running or not:
public boolean IsServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(getApplicationContext().ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

android test case. how to get the services running?

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

android how to check if service is running

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

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

How to check if an IntentService was started

I'd like to get to know if an Activity successfully started an IntentService.
As it's possible to bind an IntentService via bindService() to keep it running, perhaps an approach would be to check if invoking startService(intent) results in a call of onStartCommand(..) or onHandleIntent(..) in the service object.
But how can I check that in the Activity?
Here's the method I use to check if my service is running. The Sercive class is DroidUptimeService.
private boolean isServiceRunning() {
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
if (serviceList.size() <= 0) {
return false;
}
for (int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if (serviceName.getClassName().equals(DroidUptimeService.class.getName())) {
return true;
}
}
return false;
}
You can add a flag when constructing the PendingIntent, if the returned value was null, your service is not started. The mentioned flag is PendingIntent.FLAG_NO_CREATE.
Intent intent = new Intent(yourContext,YourService.class);
PendingIntent pendingIntent = PendingIntent.getService(yourContext,0,intent,PendingIntent.FLAG_NO_CREATE);
if (pendingIntent == null){
return "service is not created yet";
} else {
return "service is already running!";
}
I'd like to get to know if an Activity successfully started an IntentService.
If you don't get an exception in either the activity or the service when you call startService(), then the IntentService was started.
As it's possible to bind an IntentService via bindService() to keep it running
Why?
Here's the method I use to check if my service is running:
public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return service.started;
}
}
return false;
}

Categories

Resources