Answer the question
In order to leave comments, you need to log in
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;
}
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());
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question