Background Service Stopped When Activity Killed Or Stop or force close
Asked Answered
T

3

0

My Problem Is When I M Pressing Home Button And Close My App At That Time Background Service stoped And After That It Requires To LOgin Again In App Then Background Service Will Start So How Can I Do Like Whatssapp If App Will Close Or Kiiled Then Also Sevice Can Run In Background.....

Timework answered 17/3, 2014 at 19:34 Comment(1)
Do you REALLY expect smd to check repo you linked for all bugs you have made? Paste here code to start your service.Epictetus
I
4

I had the same problem. When you swipe an application out of the application tray, the process of the background service is killed.

You can add the following code in your service or see this question

@Override
public void onTaskRemoved(Intent rootIntent){
   Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
   restartServiceIntent.setPackage(getPackageName());

   PendingIntent restartServicePendingIntent =  PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
   AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
   alarmService.set(
      AlarmManager.ELAPSED_REALTIME,
      SystemClock.elapsedRealtime() + 1000,
      restartServicePendingIntent);

   super.onTaskRemoved(rootIntent);
}

What this code does is that it restarts the process of your application once it is killed when your activity is killed.

Intellect answered 17/3, 2014 at 20:27 Comment(1)
When I swipe an application out of the application tray "onTaskRemoved" call back getting called but service is not starting why?Rive
G
2

Its very simple, Just follow the scenarios in below link.

Continue Service even if application is cleared from Recent app

your service and processes (Threads run inside your service) will remain continuous.

Glockenspiel answered 7/8, 2017 at 13:8 Comment(0)
E
0

Ok, I'm just guessing - really don't want to check all code for your application.

Service can be starter in 2 ways:

  1. Using bindService() what probably you are doing here. It's nice, but means that service will be disposed as soon as the last client unbind it (what you probably doing in some onPause() method.
  2. Using the startService() command. In such case service will run as long as you stop it or system will consider that this service is using to much memory that should be used for some more urgent task (ex: some fat activity)

The quick fix for your issue (if my guesses are right) will be switching service to be run in "startService" mode. If you don't want to play much with your code the best place to put it is onBind() method of your service (just try to start service from itself). Of course this will not start the service but will switch it's mode to the "long running, real background one". This will not solve all your problems as I mentioned above - service still can be killed by system in order to get more resources. You can prevent it in some ways: if you really want to keep service running, you should consider to use foreground service (google it) that will increase chances of the service to survive. But the best option is just to use service lifecycle, save data if needed and load them on service start, just like you should do with activity. For long running background services there is also option start sticky that will cause service starting after every kill, crash etc.

Epictetus answered 17/3, 2014 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.