4

I created background service on android tab using startService() method inside the Activity. Then I create a Thread on onStart() method. I read from the android developer site that service are independent of the activity, but when I destroy the Activity android system kills the service too. I want the service to continue until I do not stop it myself.

I am aware that it can be done using AlarmManger but it consumes battery. There is no need of push notification, but the service needs to run in the background. Any suggestions on how to solve this?

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
Er. Joshi
  • 185
  • 1
  • 8
  • Are you making any call in the onDestroy()? How do you know that the service is also destroyed? Put some code, so that we can understand the isuue. – Atul O Holic Mar 13 '14 at 07:08
  • 1
    did you tried http://developer.android.com/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29 – sherpya Mar 13 '14 at 07:26

3 Answers3

0

Use this in your service class and call it by using button

 @Override
    public void onDestroy() {


           Log.e("serive destroyed", "service distroyed");
        super.onDestroy();
    }
Illusionist
  • 102
  • 1
  • 1
  • 13
0

check this link for AsyncTask won't stop even when the activity has destroyed & in that check the Answer Posted By Snicolas.And take Reference of other Answers. Hope this helps.

Community
  • 1
  • 1
i.n.e.f
  • 1,773
  • 13
  • 22
0

It should not generally happens when activity destroys service also destroys.

Services are components that run in the background, without a user interface. More important, Android services can have life cycles separate from activities. When an activity pauses, stops, or gets destroyed, there may be some processing that you want to continue. Services are good for that too.

Dasari
  • 438
  • 4
  • 11