Custom JobIntentService onHandleWork not called

2019-03-17 11:19发布

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: I tested this on an api level 21 device and it worked.. but it doesn't seem to be getting called on my android O 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?

7条回答
该账号已被封号
2楼-- · 2019-03-17 11:56

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.

查看更多
Luminary・发光体
3楼-- · 2019-03-17 12:01

I ran into this issue trying to enqueue the JobIntentService using JobScheduler. While JobScheduler has its own enqueueWork() method, it doesn't work with JobIntentService. The service will start but onHandleWork() is never called.

It started working again when I used the static enqueueWork() method that is on the JobIntentService - eg:

MyJobIntentService.enqueueWork(context, ...)

None of this was obvious from reading Android's javadoc.

查看更多
时光不老,我们不散
4楼-- · 2019-03-17 12:07

For me I was still starting the service after enqueueWork and was giving me error because of that.

查看更多
太酷不给撩
5楼-- · 2019-03-17 12:15

Just try exiting and running the Android Studio again. Then test again. In my case, the version of Android Studio is v 3.3.1. See the sample code that works properly.

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();
    }
}

// start the JobIntentService as you need.

CustomizedIntentService.enqueueJobAction(context, CustomizedIntentService.MY_ACTION);

查看更多
【Aperson】
6楼-- · 2019-03-17 12:16

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

查看更多
女痞
7楼-- · 2019-03-17 12:18

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.

查看更多
登录 后发表回答