Tell me, I wrote a simple application for android, which periodically polls your server over the network. I noticed that over time, the android completes it, as an assumption when there is not a memory grip or when the battery is low. But other applications continue to work, for example OpenVPN. Tell me which way to dig, how to make sure that the application does not end, has a high priority, or something like that!

    3 answers 3

    When the phone does not have enough RAM, it starts to destroy processes by priorities.

    If it is rude to say, the activites are destroyed first, then the services, then the services marked foreground. (Yes, my fingers will not be cut off for such an explanation by a colleague).

    To prevent this from happening, you need to transfer part of the logic to the Service, in which case the system with a smaller chance will take exactly your application to free up memory.

    UPD: enter image description here

    • Thanks for the answer, I just read about the service, but there was no certainty that this would help. It turns out no way to complete the defense? If it is not difficult, in a nutshell, tell us how the alarm clock on the phone works, for example, it doesn’t seem to end under any circumstances. - Bottom Bottom
    • @NotDonny updated the answer. Alarm clock on the phone - for starters, a standard application that goes to any phone, I can’t say for sure - only if working on the service allows you to never shut down this application. However, there is simply a service that contains the intent with the Alarm Manager, with the date and time. When the desired date and time come - then the intent starts. - Silento 2:22 pm

    Visible processes are among the highest in priority in Android, and they are higher than the system ones. The task of your service to make visible, and if more accurately bring to the fore. Using the startForeground method

    void startForeground (int id, Notification notification) 

    It is the notice that is abused by some well-known devices, which can be stopped only in the processes of the launcher.

    enter image description here

      Supplement the answers of colleagues, if you have Android 6.0, then everything becomes even more complicated.

      In this update, the so-called Doze mode and App Standby appeared.

      Doze Mode - the "shutdown" mode, in which all devices go to Marshmallow after some time of immobilization without charging. App Standby - automatic deprivation of applications for access to device resources, all that the user has not opened for a long time.

      Here you can read more Habr

      Here you can listen to the innovations of 6.0, about the above modes, roll closer to the end of AndroidDev