Sartheris Stormhammer
Sartheris Stormhammer

Reputation: 2564

Android Foreground Service check if created

I have a service in my app, its a medicinal app and the service is supposed to run the whole time the app is installed without being killed. So I tried few approaches, tried with a regular service returning START_STICKY but that didnt exactly recreated the service as it is supposed to be. Then I decided to try it with foreground service. I am creating it win the service onStartCommand like this

    startForeground(FOREGROUND_ID, buildForegroundNotification());
    return START_STICKY;
}

private Notification buildForegroundNotification() {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    b.setOngoing(true);
    b.setContentTitle("My App");
    b.setContentText("Therapy is running");
    b.setSmallIcon(R.drawable.ic_launcher);
    return (b.build());
}

so I have two questions: - Can the foreground service be killed by Android? - How do I check if the notification is already created so it doesn't create it every time the service is starting (the service is being recreated multiple times when the Activity is also running, because it is actively communicating with it)

Upvotes: 0

Views: 393

Answers (1)

fiipi
fiipi

Reputation: 663

For the first question, see here.

Second question: I suppose you're using myNotificationManager.notify(int id, Notification notification). The ID field is used to identify the types of notification you want to display, so setting the same ID for the same type of notification will replace the old notification with the newly created one (if the old one is still displayed) or simply display a new one (see NotificationManager docs).

Upvotes: 1

Related Questions