0

In my project I am using a service that returns START_STICKY in the onStartCommand function. I use the service to run some background processing in an asynctask (which I call from the onStartCommand function). I need to kill the service, because it keeps running when I check the app settings on my phone.And set the AlarmManager to call the service again after X hours.

I have tried

if (isMyServiceRunning()) {
   stopService(new Intent(context, MFilterIt.class));
   }



private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager
            .getRunningServices(Integer.MAX_VALUE)) {

        if ("com.sixdee.mfilterit.MFilterIt".equals(service.service
                .getClassName())) {

            return true;
        }
    }
    return false;
}

I call this in my asynctask.so I see this behaviour.if the delay that I set in AlarmManager is 2.The delay becomes 6 (2*3).I see this behaviour only when I killing the service in the above mentioned fashion

Is there any other way or am I doing any wrong.

umalogic
  • 63
  • 5
  • Um, why not just call `stopService()` instead of wrapping it in `isMyServiceRunning()`? You might also consider whether `START_STICKY` is what you really want here. – CommonsWare Jun 20 '16 at 20:32
  • I need to explicitly start and stop my service when needed so i think i am correct in using `START_STICKY`.Did i miss anything – umalogic Jun 20 '16 at 20:33
  • i tried `stopService()` it behaves the same. – umalogic Jun 20 '16 at 20:34

0 Answers0