How to start a service with foreground priority?
Service always runs in background since it doesn't have UI, so its not possible.
startService(intent, boolean foreground); pass second parameter true, to make this service as foreground.
Set priority to service in <service> tag in the manifest file. if priority is any thing other than 0, then it will be treated as foreground service.
startForeground (int id, Notification notification), use this function in onCreate() of your service.
Generally services will run in background, which is of 3rd priority. if you feel that the service is critical for user, then you can increase its priority by making it foreground service. Use function startForeground (int id, Notification notification), in onCreate() of your service to make this service as foreground service.
Foreground services will be treated with highest priority, so android will ensure it will not kill these services even in case of low memory situations. Eg: MP3 player service is a foreground service.
Thank you, I was looking for this!
Back To Top