1

I've a Service that is called from an Activity with this code:

startService(new Intent(AMC_Activity.this,CallService.class));

Service is running good for about 20-30 minutes, but after that service stop running, I know that I can use 'foreground' service, but by using that I should show a notification, so, is there any other way to prevent service stop running?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Reza_Rg
  • 3,345
  • 7
  • 32
  • 48
  • Anything useful in LogCat when service stops? – Voicu Apr 27 '13 at 19:50
  • I've put the conclusions of quite some investigations of service stopping at http://stackoverflow.com/questions/15435117/service-being-re-created-by-alarmmanager. I presume that you return `START_STICKY` from `onStartCommand`? – Neil Townsend Apr 27 '13 at 19:54
  • 1
    Mybe it can solve your problems: http://stackoverflow.com/a/5766794/1001401 . But stopping service is a normal for android, maybe you need to use your service another way. – nfirex Apr 27 '13 at 19:58

2 Answers2

0

I am not sure but i think you are starting your service from UI thread and after that you are putting your in application background so after sometime your activity instance getting lost and that UI thread also killed at that time.

Solution

Create a new Thread and start service with that Thread instead of UI Thread, because service is doing work on that Thread whoever Thread is invoking it.

//This code will be in class body.
private Thread thread1 = new Thread(){
    public void run(){
        startService(new Intent(AMC_Activity.this,CallService.class));
    }
}

//Now this code will be call when you are going to start the service, i.e. Under onCreate()
thread1.start();

It may helpful to you.

Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38
0

If you use a TaskManager App on your device you should take care that it doesn't kill your app and/or service too.

Bongoking
  • 3
  • 2