Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom JobIntentService onHandleWork not called

I recently was updating an app that I work on to handle notifications from push using a JobIntentService instead of a regular IntentService because it seems like the correct way to handle this on pre-Lollipop devices as well as post. I am enqueueing 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 any callbacks in onHandleWork or any error logs in my logcat. Has anyone successfully integrated this that could help?

Update 1

I tested this on an API level 21 device and it worked.. but it doesn't seem to be getting called on my Android Oreo Pixel XL device.. Any clues as to why?

Update 2

Also I seem to be seeing the IntentService's onCreate be called, but none of the other lifecycle methods (including onHandleWork). Has anyone encountered this either?

like image 466
Eshaan Avatar asked Sep 12 '17 00:09

Eshaan


4 Answers

I had the same issue after upgrading from IntentService to JobIntentService. Make sure you remove this method from your old implementation:

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

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

like image 185
agirardello Avatar answered Nov 01 '22 16:11

agirardello


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

My current theory is that my initial setup did not work; my current one does and just redeploying new code does not clear out the broken state from the JobScheduler; a reboot or an uninstall/reinstall of the package does.

The setup that's working now (migrated from a former 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 ComponentName is not set).

like image 39
Jule Avatar answered Nov 01 '22 16:11

Jule


If you have overridden the onCreate method in your JobIntentService, it will prevent the onHandleWork to be called.

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

like image 3
Dimitar Darazhanski Avatar answered Nov 01 '22 16:11

Dimitar Darazhanski


This is what worked for me,

Remove the 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);
}

Have no idea why this worked.

like image 3
Sanket Berde Avatar answered Nov 01 '22 16:11

Sanket Berde