Background Service Stopped When Activity Killed Or

2019-09-17 11:00发布

问题:

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.....

回答1:

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.



回答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.



回答3:

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.