So I'm not sure where/how to implement this method to make my service run in the foreground. Currently I start my service by the following in another activity:
Intent i = new Intent(context, myService.class);
context.startService(i);
And then in myServices' onCreate() I try the startForeground()...?
Notification notification = new Notification();
startForeground(1, notification);
So yeah I'm a bit lost and unsure of how to implement this.
Add given code Service class for "OS >= Build.VERSION_CODES.O" in onCreate()
Add this permission in manifest file:
If you want to make IntentService a Foreground Service
then you should override
onHandleIntent()
like thisHow to make notification ?
simple. Here is the
getNotification()
MethodDeeper Understanding
What happens when a service becomes a foreground service
What is a foreground Service ?
A foreground service,
makes sure that user is actively aware of that something is going on in the background by providing the notification.
(most importantly) is not killed by System when it runs low on memory
A use case of foreground service
Implementing song download functionality in a Music App
In addition to RAWA answer, this peace of code:
You can change to:
If you will look inside this method then you can see that this method do all checking work for you.