I have recently replaced all my service to foreground services and JobIntentService since there are some background execution limits (https://developer.android.com/about/version
Well..., Its a lot big theory...!! It would not be able to put it all here. I will try my best which will make some your concepts clear.
I have already lost my 2 complete years in reading google documentations... Which are use-less
... With no proper documentation
and with no proper sample codes for its developers
..!! So i mention this in every of my posts on stack-overflow
, As it will help to save time of others..!!
It looks you are a good programmer; just need some hints to your posted question
:
Hint-1 :
YOU :- I have recently replaced all my service to foreground services and JobIntentService
foreground service :
If you need ALL THE TIME RUNNING PROCESS; WHICH WILL NEVER END... ONCE IT IS STARTED
it is used in service which returns START_STICKY
from its OnStartCommand
. Which is again not advised to use as if you want to implement it at any cost ... then you will have to use a notification with setOngoing(true)
Which end user would not be able to swipe away your notification, it will remain there forever....
Use of the foreground service :
There has been restrictions on receivers too; above Oreo
onwards and you can not use all the receivers and intent actions by declaring it in manifest and by just making a receiver... I advice to just use BootComplete
permission and use a single receiver
which receives the boot_completed
intent and calls a service
if below O and calls a foreground service above O. Now from that foreground service you implement the runtime receivers for all and unregister it in Ondestroy methods. I have never found an official sample code for implementing runtime receiver and finally i have implemented it successfully by many months hard-work... Yes it was not a smart work due to google
When to use foreground service :
Only if you want to implement broadcast receivers.... If you do not want to implement any broadcast receivers; STAY AWAY.......
Hint-2 :
YOU :- I have recently replaced all my service to foreground services and
JobIntentService
** service has its quality of :**
Just doing a very tiny work... and just exit... it has to be exited by StopSelf()
... Again, Services can cause data-loss
if called multiple times... As same service thread can be run more than once... Again if you want a service to do a lot of work... Use START_STICKY
... But again it is not recommended and i have suggested already, when to use it in Hint 1.
** Intentservice has its quality of :**
Doing a relatively long running tasks and it has property of execution serially only
If you again and again calls the same intentService
, then all calls will be kept in a queue
and will be executed one by one
after finishing one by one
. Which is not the case in service as depicted above. It ends on its own... no need to end it by a developer..!!
** Unique Quality of all :**
Once they are crashed
android can stop them calling in future without notifying you as it crashes the app. Need to be handled them with try-catch-exception
to avoid crash
. Again... If you are implementing threads within services
then try-catch-exception
will not save your application from being crashing
...
** THEN WHAT THE HELL & HOW TO IMPLEMENT IT THEN :**
Use FireBaseJobScedular
:-
EVEN ALL THE TIME RUNNING TASK
EVEN SUPPORTED BY NON STANDARD COMPANIES
like vivo, mi, oppo, one+3, ... which takes stock-android
makes changes to it and gives names like FunTouchOs, ColorOs, OxygenOsGooglePlyService
and runs within it, And obviously non-standards companies too would not restrict google apps from being doing its tasks.Oreo
.., Even i have tested it on Android P
and works below Android version 5.0 as AlarmManager
tasks.minsdk above 16
, target sdk 26
as if in case you wants to upload your app to google play
it is compulsory now and that news would have been heard you. and compile sdk 26
.JobService
in manifest and use a single line permission of receive_boot_complete
cold boot
and hot boot
you can focus on actual tasks
.return false
to indicate task has been finished and it will end the JobService.Why i am suggesting because i am CTO
of a well-UNKNOwn
company and has been experienced the problems caused by foreground service
across the many types of android phone manufacturers... It is not the Apple and ios
so we had to experienced it. Remain developer since past 18 years and i mostly codes today too... in all of the development projects and its development strategies are thought by me only.
Correct me ... too... As you have not mentioned
what your tasks
andproject
is related to... and what you exactly wants to be done in aforeground service and intent-service
... Let me know..., It would be my pleasure to help you. It is a general theoretical answer rather than what you wants.... But for giving you actual answer i will need exact your project scope..