X
X
xmdv2019-01-27 15:36:08
Android
xmdv, 2019-01-27 15:36:08

Why does the service in the Android application stop?

Good day! I'm writing an Android application that needs to perform some long-running tasks in the background (including when the application itself is minimized). For simplicity, we will assume that a long-running task consists in the fact that once a second a certain counter is incremented, and the service sends a notification showing the value of the counter. I read that you need to use services to implement such tasks. I implemented a service, inherited it from IntentService and described the logic of work in it along with sending notifications to the user. The problem is that if you exit the application, then after a while the service will stop, and notifications will stop coming. Googling has shown that this is a fairly common problem, and it is solved by returning the START_STICKY or START_REDELIVER_INTENT flag from the onStartCommand method (I tried both flags). Unfortunately, this method did not help me - the service is still stopped by the operating system after some time. The code is written in C# (I use xamarin), but I hope that there is nothing incomprehensible for people writing in java. The code looks like this:

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
    base.OnStartCommand(intent, flags, startId);     
     
    var thread = new Thread(new Runnable(() => 
    {
        for (int i = 0; i < 10_000; i++)
        {
            ShowNotification("channel", "Search", $"second: {i}, threadId: {id} startId: {startId}");
            Thread.Sleep(1000);
        }                           
        StopSelf(startId);
    }));
    thread.Start();

    return StartCommandResult.StartSticky;
}

The ShowNotification method looks like this:
protected void ShowNotification(string channel, string title, string message)
{
    Intent resultIntent = new Intent(this, GetType());
    resultIntent.AddFlags(ActivityFlags.NewTask);

    var resultPendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);

    var mBuilder = new NotificationCompat.Builder(this);
    mBuilder.SetSmallIcon(Resource.Mipmap.ic_launcher)
        .SetContentTitle(title)
        .SetContentText(message)
        .SetAutoCancel(false)
        .SetContentIntent(resultPendingIntent);

    if (!(GetSystemService(NotificationService) is NotificationManager mNotificationManager))
        return;

    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
    {
        var importance = NotificationImportance.High;
        var notificationChannel = new NotificationChannel(channel, channel, importance);
        notificationChannel.EnableLights(true);
        notificationChannel.LightColor = Color.Red;
        notificationChannel.EnableVibration(true);
        notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });
        mBuilder.SetChannelId(channel);
        mNotificationManager.CreateNotificationChannel(notificationChannel);
    }
    mNotificationManager.Notify(0, mBuilder.Build());
}

The service works for some time (sometimes a minute, sometimes five), sends notifications every second, but if you exit the application, sooner or later it stops working, and new notifications no longer come. Maybe someone knows how to solve this problem and make the service run constantly, until the user stops it or he finishes doing his work? Well, or how to force the service to restart every time after the OS has stopped it?
As far as I understand, this problem is solvable, because there are many applications that quietly work in the background. For example, torrent clients that can download a file for several hours and display notifications about the progress of the download. Well, or music players that play music in the background, while the music is not interrupted (that is, the service is most likely not stopped by the operating system).
How is this implemented in other applications and what is my mistake (well, besides the fact that I use Xamarin, it is already difficult to do something about it) ? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2019-01-27
@402d

> I implemented a service, inherited it from IntentService
Forget about using IntentService, it stops as the app stops because it runs on the same process as the app.
https://stackoverflow.com/questions/44114517/long-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question