Custom JobIntentService onHandleWork not called

I recently updated the application I'm working on to handle notifications using JobIntentService instead of the usual IntentService, because this seems like the right way to handle this on pre-lollipop devices as well as post. I start the work as such:

enqueueWork(context, MyJobServiceExtension.class, JOB_ID, work);

This is the manifest declaration:

<service android:name="com.example.MyJobServiceExtension" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true" tools:node="replace">

I never see callbacks in onHandleWork or any error logs in my logcat. Has anyone successfully integrated this that could help?

Update: I tested this on a level 21 api device and it worked. But it looks like this is not like calling my Android device with an Android pixel XL device. Any clues as to why?

Update # 2: I also see that the intentservice onCreate call is called, but none of the other lifecycle methods (including onHandleWork). Has anyone come across this?

+25
android android-8.0-oreo
source share
10 answers

I had the same problem after upgrading from IntentService to JobIntentService. Make sure this method is removed from the old version:

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

For me, this solved the problem, and now it works on both pre- and post-Oreo.

+51
source share

I had the same problem (worked fine on the pre-O device, no indication of anything, whatever happens on the O-device). Today I tried again with the same code as yesterday, now it works - the only difference is that I rebooted the device between them.

My current theory is that my initial setup did not work; my current one does and simply redistributes the new code, does not clear the broken state from JobScheduler; reboot or uninstall / reinstall package.

A setting that now works (migrated from the previous IntentService):

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

and start with

 Intent intent = new Intent(); intent.putExtra(EXTRA_NAME, extraValue); JobIntentService.enqueueWork(context, MyJobIntentService.class, FIXED_JOB_ID, intent); 

Note that the intent is not an explicit intent (i.e., the component name is not set).

+7
source share

I ran into this problem while trying to assign the value of JobIntentService using JobScheduler . Although JobScheduler has its own enqueueWork () method, it does not work with JobIntentService . The service will start, but onHandleWork () is never called.

It started working again when I used the static enqueueWork () method, which is located in JobIntentService - for example:

MyJobIntentService.enqueueWork(context, ...)

None of this was visible when reading Android javadoc.

+2
source share

This is what worked for me

Remove IBind Override as suggested by @agirardello

and added the following

  @Override public int onStartCommand(@Nullable Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } 

I don't know why this worked.

+2
source share

If you override the onCreate method in your JobIntentService , this will prevent the onHandleWork call.

I converted my Service to JobIntentService and only after removing the onCreate method onCreate it onCreate .

+2
source share

I ran into a somewhat similar problem when onHandleWork not called a second time after switching from Service to JobIntentService . The logs showed that enqueueWork was called, but onHandleWork did only the first and it looks like onHandleWork .

After a few more digging and registration, I found that the difference is that JobIntentService#onDestroy exists in the β€œstuck” script, although all operations in onHandleWork were completed and seemingly completed.

It turned out that the bindService had a call to the bindService this service for the activity life cycle, which prevented the utilization of the first job and for some reason called enqueueWork after this condition called the " enqueueWork service and never started any of the following onHandleWork .

So here is the wrong event log in which the JobIntentService will be the JobIntentService after the first call that never starts onHandleWork again:

 enqueueWork -> first call onHandleWork started (log in the first line) onHandleWork finished (log in the last line) enqueueWork -> second call enqueueWork -> third call 

And here is the correct event log with JobIntentService working correctly after deleting the bindService call:

 enqueueWork -> first call onHandleWork started (log in the first line) onHandleWork finished (log in the last line) onDestroy (service is destroyed after the job is finished) enqueueWork -> second call onHandleWork started (log in the first line) onHandleWork finished (log in the last line) onDestroy enqueueWork -> third call onHandleWork started (log in the first line) onHandleWork finished (log in the last line) onDestroy 

Hope this will be helpful to someone.

+1
source share

For me, I was still starting the service after enqueueWork and gave me an error because of this.

0
source share

Just try to exit and start Android Studio again. Then check again. In my case, version of Android Studio v 3.3.1. Check out the sample code that works correctly.

 public class CustomizedIntentService extends JobIntentService { public static final String MY_ACTION = "action.SOME_ACTION"; private static final int MY_JOB_INTENT_SERVICE_ID = 500; public CustomizedIntentService() { } // Helper Methods to start this JobIntentService. public static void enqueueJobAction(Context context, String action) { Intent intent = new Intent(context, CustomizedIntentService.class); intent.setAction(MY_ACTION); enqueueWork(context, CustomizedIntentService.class, MY_JOB_INTENT_SERVICE_ID, intent); } @Override protected void onHandleWork(@NonNull Intent intent) { String action = intent.getAction(); // action will be "action.SOME_ACTION" } @Override public void onCreate() { super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); } @Override public boolean onStopCurrentWork() { return super.onStopCurrentWork(); } } 

// run JobIntentService as needed.

CustomizedIntentService.enqueueJobAction (context, CustomizedIntentService.MY_ACTION);

0
source share

Funny as it may seem, I had a similar problem because I did not change the class name to its own name in enqueueWork (), because I copied the code from one of my classes. After I made the update, it started working correctly.

0
source share

I finally found a solution to this LoL problem

If you override the onBind method and call the work using the enqueueWork method, you need to return the binding to the work engine that does this:

 @Override @Nullable public IBinder onBind(@NonNull Intent intent) { [... Do What You Want ... ] return super.onBind(intent); } 

We return the IBinder of the super.onBind method, so you should use it to bind to JobIntentService.

If you want to bind and return another binder, you can do this:

 @Override @Nullable public IBinder onBind(@NonNull Intent intent) { IBinder binder = initSynchronizer(); new Thread( () -> onHandleWork(intent) ).start(); return binder; } 

So by launching you "onHandleWork" in another topic. So you can use:

"bindService (....., JobIntentService.BIND_AUTO_CREATE);"

contact the service and return your Binder. In any case, when you disconnect from the service, the service will be killed, and if it still works, you will not be able to bind it again because the service was killed, but the thread in which "onHandleWork" is still running ...

Therefore, I suggest that you use this version only if you need to complete a task that must interact with the activity until it becomes active, and should still work if the activity is destroyed (without the ability to bind jobService again but just to start a new one ...)

In order not to kill the service after unlinking, you need to run it in the "foreground" in the "stopForeground" in "onDestroy". That way, you are still serving only for the thread that processes the "onHandleWork" methods.

I hope that Google will solve this damn fast LoL, I have converted all the old "Service" and "IntentService" to new jobs, but ... they work really worse than before!

So far, enjoy coding;)

0
source share

All Articles