0

In my application, I'm starting a Service multiple times even when it's running. Its a service that is perpetually running.

My question here is, when I call stopService(), will it stop the current running Service and ALSO clear the stack of the waiting intent? Because I want to be sure it is not running anymore.

dev
  • 11,071
  • 22
  • 74
  • 122
Jaythaking
  • 2,200
  • 4
  • 25
  • 67
  • 1
    You have an IntentService which is perpetually running ? This does not seem likely. – dev May 26 '16 at 19:48
  • Your are totally right, I made a typo, it's actually a Service – Jaythaking May 26 '16 at 19:49
  • Why you are starting your service multiple times , even its a Sticky service. Can you explain it, i want to think about it. There is so many ways to trigger your service methods without recreating. – Yasin Kaçmaz May 26 '16 at 19:54
  • 1
    I wrote it before but I erased it since it was confusing people... It's because when my app is force shutdown and reopen, I couldn't find any bulletproof solution that check if a service is still running so I ended up just calling startService again, even if it is already started... But now i'm wondering if stopService will stop it for good if there was multiple call to startService that were made. So here I don't really want to know how to check if a service is running since I already read all the solution provided here, I want to know how to stop a service that was started multiple times – Jaythaking May 26 '16 at 20:00
  • 1
    This comment better than your question. Now i understand your question/problem. But i am saying again in my opinion you doing it wrongly. When you created a sticky Service it will automatically restarting when low memory etc. All you need to do is start service once and check for running in Application class , or in starter Activity. – Yasin Kaçmaz May 26 '16 at 20:07

1 Answers1

0

I dont stop my Service, it is STICKY Service. When starting app i am controlling the service is running or not like :

public static boolean isMyServiceRunning(Class<?> serviceClass,Context mContext) {
    ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

Usage :

if(!isMyServiceRunning(MyService.class, getApplicationContext())){
       startService(new Intent(this, MyService.class));
}

But when i logout my user i am stopping service like :

Intent serviceIntent=new Intent(mContext,MyService.class);
mContext.getApplicationContext().stopService(serviceIntent);
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
  • I tried that one also... Makes my application crash for some particular use cases, and is not recommended by many user. See that link : http://stackoverflow.com/a/5921190/3457218 – Jaythaking May 26 '16 at 19:35
  • If you read that answer its sayin : "This works reliably, because it is based on the information about running services provided by the Android operating system through ActivityManager#getRunningServices." I think getting some information that provided by Android System is more reliable for me – Yasin Kaçmaz May 26 '16 at 19:38
  • 1
    Read through the comments there... "This method is not really sustainable. It does not work on Android KitKat.", "this method is only intended for debugging or implementing service management type user interfaces." It's not meant for control flow! " ... I was actually using it before, until it makes my app crash – Jaythaking May 26 '16 at 19:40
  • I didnt get any issue when running my app in Kitkat devices. There is no proof attached in comments. Now i have doubts about too :( – Yasin Kaçmaz May 26 '16 at 19:49
  • When my app crashed, it pointed to that method so I don't trust it anymore :/ – Jaythaking May 26 '16 at 20:02
  • I think you are right , but if you come with logs , errors and code maybe i will compare yours to mine. – Yasin Kaçmaz May 26 '16 at 20:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113069/discussion-between-jaythaking-and-yasin-kacmaz). – Jaythaking May 26 '16 at 20:12