How to check if an IntentService was started - android

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

Related

check service is running from an activity class

I want to know whether the following method will work or not, I've got this method in stack-overflow itself but it doesn't seem to work.
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 have a Job Service, which will be executed every 15 minutes for this the I am using Job scheduler to trigger the service with whatever time is mentioned.
I have a method as executeService() as follows:
public JobScheduler initJob(Context context){
if(jobScheduler == null){
jobScheduler = (JobScheduler)context.getSystemService(JOB_SCHEDULER_SERVICE);
}
return jobScheduler;
}
public void executeService(){
initJob(this);
ComponentName jobService = new ComponentName(this.getPackageName(), OfflineService.class.getName());
JobInfo jobInfo = new JobInfo.Builder(2,jobService).
setPersisted(true).
setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).
setPeriodic(millisecondsQueue).
build();
int jobId = jobScheduler.schedule(jobInfo);
}
Now this executeService() is called every time the user comes to MainActivity, onResume of MainActivity I've put a check as below
if(!isMyServiceRunning(OfflineService.class)){
executeService();
}
If my service is already triggered then after I come back to MainActivity the method isMyServiceRunning() returns false always.
I've gone through all the questions-answers related to this but I ended up writing this question again I am using Android OS Nougat.Any help would be great.

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

How to rebind to foreground service?

A foreground service is started and immediately bound to. Is there a way to know whether the serivce has been stopped or is still running after an activity restart. The safe way would be to execute startService anyway, but is there a cleaner one?
You can use this method from:
https://stackoverflow.com/a/6452570/5457878
To check if your service is still running. You can call this method in your onResume() or onCreate(), and if it is not running, I would restart it.
private boolean isServiceRunning(String serviceName){
boolean serviceRunning = false;
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
while (i.hasNext()) {
ActivityManager.RunningServiceInfo runningServiceInfo = i
.next();
if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning = true;
if(runningServiceInfo.foreground)
{
//service run in foreground
}
}
}
return serviceRunning;
}

Check if service is running not behaving right

I created a boolean to check if a service is running, if it is running I want it to show linearlayoutA and if it is not running I want it to show another linearlayoutB.
The problem is this, it shows the right linear layout when I start the activity, if the service is running it shows linearlayoutA if its not running it shows linearlayoutB but when I start the service in the activity it shows linearlayoutB and does not change to A even when the service as stopped until I close the application and open it. method for checking
public boolean isRunning(Class<? extends Service> serviceClass) {
final Intent intent = new Intent(TimerActivity.this, serviceClass);
return (PendingIntent.getService(TimerActivity.this, CODE, intent, PendingIntent.FLAG_NO_CREATE) != null);
}
This is how I call it in onCreate of the activity
if(isRunning(TimerLocationService.class)){
setWaitScreen(true);
}else{
setWaitScreen(false);
}
try this.
private Boolean isServiceRunning(String serviceName) {
ActivityManager activityManager = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : activityManager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceName.equals(runningServiceInfo.service.getClassName())) {
return true;
}
}
return false;
}
I hope this is helpful for you.

compile error on method to check if service is running

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

Categories

Resources