Good day. Tell me why the work of the background service on Android can fall off. No error messages occur, the service simply stops working several days after launch. How can you debug this problem when working on the device (find out the reason for the shutdown), or, perhaps, the OS itself stops? I came across this issue at the implementation stage (different types of services were indicated and the priority of their work). In the case of the OS (lack of resources or anything else) - how can this be solved?

Itself is far from mobile development, so I will be grateful for any information. Thank.

    2 answers 2

    That's right, the system essentially decides when to stop your service. Here more about the work of services on xamarin.

    When I encountered such a problem, a change in the StartCommandResult value in StartCommandResult.Sticky helped me.

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { return StartCommandResult.Sticky; } 

    The idea of ​​the parameter is that when the service stops, the system will re-raise it.

    • Yes. I also have OnStartCommand returns StartCommandResult.Sticky However, in my case, this solution did not help. - Vitaly D

    Here you cannot change anything, as the system decides on the basis of other processes and if it needs resources, then everything will be complete.

    Make sure your service does not consume a lot of resources. So you can only minimize the likelihood of its shutdown. (In principle, apply to all mobile systems because the balance of performance and autonomy is important)

    A good article on the work of Android (see section App Lifecycle) https://habrahabr.ru/company/solarsecurity/blog/338494/

    Yes, and the option from @Mikhail Efremov is a good option, since it is designed to restart services (for example, the background music playback service), but even standard audio player applications can close in a day or two if the system needs resources.