It is necessary to make my Service not killed by the system and garbage cleaners.

I execute such code:

 public void onCreate() { super.onCreate(); Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher); builder.setContentTitle("Hello"); Notification notification; if (Build.VERSION.SDK_INT < 16) notification = builder.getNotification(); else notification = builder.build(); startForeground(777, notification); startService(); } 

It seems that both the picture and the label appear in the top menu, but when the cleaning is started, the Service dies and does not turn on until you start it manually.

  • one
    "garbage cleaners" GC schA shed a few tears - katso
  • 3
    @katso, I think this is not about GC, but programs that are pseudo-clean-RAM-junk - Vladyslav Matviienko

1 answer 1

If you need the service not to kill when you start Notification from it, use the startForeground () method

  • honestly did not understand, I seem to be using it, maybe I am using it in the wrong place ??? tell me where - Nikolay
  • judging by the written methods, you call startForeground () in the activity, and this must be done in the service itself. - s_klepcha
  • There is no this service code when it is created - Nikolay
  • make sure that the stopForeground () method is not called before you use cleaners. And, most importantly, maybe the fact is that you have no logic in Notification? - s_klepcha
  • But you can give an example of such a non-killing service, otherwise Google only shows the code, but for some reason it does not work for me ((( - Nikolay