Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often in Android 8 & above

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often in Android 8 & above



I have recently replaced all my service to foreground services and JobIntentService since there are some background execution limits (https://developer.android.com/about/versions/oreo/background) in oreo and above. As per documentation, JobIntentService acts like Intent Service for Android 7 & below and acts like JobScheduler for Android 8 & above. I have noticed there is an issue in new JobIntentService provided by Google.



Android 8 & above:



There is a crash happening continuously in android 8 and above. There was a ticket raised here mentioning about the same issue https://issuetracker.google.com/issues/63622293 and I have added a temp fix suggested by few geeks.



Android 7 & below:
JobIntentService which acts like Intent Service is not getting stopped once the work is done.



I have implemented JobIntentService within a service which triggers whenever some action is performed by a user.



Code


public class SampleJobIntentService extends FixedJobIntentService {

public static void postData(Context context, String data)
Intent intent = new Intent(context, SampleJobIntentService.class);
intent.setAction(INITIAL_ACTION);
intent.putExtra(SAMPLE_ID, data);
SampleJobIntentService.enqueueWork(context,intent);


public static void enqueueWork(Context context, Intent work)
SampleJobIntentService.enqueueWork(context, SampleJobIntentService.class, JOB_ID, work);

@Override
protected void onHandleWork(@NonNull Intent intent)
if (intent != null)
SampleRequest sampleRequest = requests.get(intent.getAction());
if (sampleRequest != null)
try
// perform some networking operations
catch (Exception ex)
Log.d("Error for intent ");

Log.i("send action ");
else
Log.e("action not found for ");





To avoid the crash with JobIntentService, I took few references from https://issuetracker.google.com/issues/63622293


public abstract class FixedJobIntentService extends JobIntentService

@Override
GenericWorkItem dequeueWork()
try
return new FixedGenericWorkItem(super.dequeueWork());
catch (SecurityException ignored)
doStopCurrentWork();

return null;


private class FixedGenericWorkItem implements GenericWorkItem
final GenericWorkItem mGenericWorkItem;

FixedGenericWorkItem(GenericWorkItem genericWorkItem)
mGenericWorkItem = genericWorkItem;


@Override
public Intent getIntent()
if (mGenericWorkItem != null)
return mGenericWorkItem.getIntent();

return null;


@Override
public void complete()
try
if (mGenericWorkItem != null)
mGenericWorkItem.complete();

catch (IllegalArgumentException ignored)
doStopCurrentWork();








Why are u triggering a service to trigger a JobIntentService. JobIntentService can be triggered directly on user action ?
– Ankur
Aug 6 at 13:20





@Ankur Even if I trigger JobIntentService directly, it is not getting killed once the work is done. I have mentioned one scenario where it is used in my project. I have a couple of JobIntentServices used in my project
– Kalai.G
Aug 6 at 13:39






Can u provide with the sample code of JobIntentService which you are using with the trigger code ?
– Ankur
Aug 6 at 13:48





I have updated my question
– Kalai.G
Aug 6 at 14:15





@Kalai.G, Okay... I got it... I am explaining it step by step as answer ... as i don't see much space left here... and i do not want to make complete thread full of like chit chat.
– sandhya sasane
Aug 8 at 11:25





2 Answers
2



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..!!


use-less


no proper documentation


no proper sample codes for its developers


stack-overflow



It looks you are a good programmer; just need some hints to your posted question :


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....


ALL THE TIME RUNNING PROCESS; WHICH WILL NEVER END... ONCE IT IS STARTED


START_STICKY


OnStartCommand


setOngoing(true)



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


Oreo


BootComplete


receiver


boot_completed


service



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


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.


topSelf()


Services can cause data-loss


START_STICKY



** 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..!!


property of execution serially only


intentService


queue


one by one


one by one



** 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...


crashed


try-catch-exception


avoid crash


you are implementing threads within services


try-catch-exception


will not save your application from being crashing



** THEN WHAT THE HELL & HOW TO IMPLEMENT IT THEN :**



Use FireBaseJobScedular :-


FireBaseJobScedular


EVEN ALL THE TIME RUNNING TASK


EVEN SUPPORTED BY NON STANDARD COMPANIES


stock-android


GooglePlyService


Oreo


Android P


AlarmManager


minsdk above 16


target sdk 26


google play


compile sdk 26


JobService


receive_boot_complete


cold boot


hot boot


you can focus on actual tasks


return false



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.


CTO


UNKNOwn


foreground service


Apple and ios



Correct me ... too... As you have not mentioned what your tasks and
project is related to... and what you exactly wants to be done in a
foreground 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..


what your tasks


project


foreground service and intent-service





I would like to thank you for addressing my issue. As per my project requirement(which I cannot disclose publically), I have to use foreground service and this service will trigger new Jobintentservice which sends metric data and this is not getting killed automatically(Intentservice used to do). As per your suggestion, If I use, firebasejobscheduler this issue will get rid off? Let me give a try. Thanks a ton again
– Kalai.G
Aug 9 at 7:29






Issue will be issue until there are programming and dependancies error/s... Just implementing "firebasejobscheduler" won't solve your problem... These are paradigms ...
– sandhya sasane
Aug 9 at 12:07





Agree with that. As per doc, JobIntentService will act like intent service and job scheduler. IntentService which I used earlier will stop once the work is done, when I replaced with JobIntentService didn't stop in < android 7 and that drains the battery like hell. I can see those service in running services as well.
– Kalai.G
Aug 9 at 12:16





Why sticked with "JobIntentService" only...? Rather than, Service, or IntentService? Or what made you to decide, i will use and will go with "JobIntentService" only ..!! Please Specify..
– sandhya sasane
Aug 9 at 12:28





Background execution limit can be crossed with either foreground service or if you do not want a STICKY notification then one should use alarmmanager... Again with this you can run long running tasks but not a FOREVER task. Again on evry reboot alarms sets are not remembered by system so need to handle it with database and on startup again set alarms...
– sandhya sasane
Aug 9 at 15:12



I think you just need this much of a code. Create a new Class MyJobIntentService and write this much of a code and call postData() to start your service.


public class MyJobIntentService extends JobIntentService

public static void postData(Context context, String data)
final Intent intent = new Intent(context, MyJobIntentService.class);
intent.setAction(INITIAL_ACTION);
intent.putExtra(SAMPLE_ID, data);
enqueueWork(context, MyJobIntentService.class, 1000, intent);


@Override
public IBinder onBind(Intent intent)
return null;


@Override
public void onDestroy()
Ln.d("Cancelled service");
super.onDestroy();


@Override
protected void onHandleWork(@NonNull Intent intent)
if (intent != null)
final SampleRequest sampleRequest = requests.get(intent.getAction());
if (sampleRequest != null)
try
// perform some networking operations
catch (Exception ex)
Log.d("Error for intent ");

Log.i("send action ");
else
Log.e("action not found for ");






And make sure to add your service in manifest file


<service
android:name="service.MyJobIntentService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />





Have you gone through this issue tracker? issuetracker.google.com/issues/63622293
– Kalai.G
Aug 7 at 6:25





Yeah i have read it whole. But i am not facing any crash issue from my users. The only issue i am facing is, the service doesnt start if many call are made to it and its happening on OREO device
– Ankur
Aug 7 at 6:57





That is true. My app used to make multiple calls to send metrics and this crash is happening often. Have tested that the JobIntentService is stopped once the work is done in android 7 and below?
– Kalai.G
Aug 7 at 7:56






Will update you with the solution if found. Thanks
– Ankur
Aug 7 at 8:10





Based on the comments it sounds like this is not an valid answer, or it has not been tested? If that is the case it should be removed.
– StarWind0
Aug 15 at 20:07






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard