I recently was updating an app that I work on to handle notifications from push using a JobIntentService
instead of a regular IntentService
because
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).
For me I was still starting the service after enqueueWork and was giving me error because of that.
For everyone who couldn't solve the problem with the other answers:
Try using different JOB_IDs every time enqueueWork is called. If a previous job hasn't finished, the Service may just be stuck (similar to the problem the user "git pull origin" has described) and a new job with a different ID may solve this issue.
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.
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);
I think that I have such problem since I try to toast some text inside onHandleWork()
but actually the problem was that it was wrong. I should use a Handler
. It may be the problem if one uses for example AsyncTask
subclasses to execute on another thread inside onHandleWork()
which is a very bad idea.