1

I need to handle intents posted via AlarmManager both while my app runs and at boot time. I've written a subclass of JobIntentService to handle the intents but it's not working as expected: onHandleWork is not called.

My implementation worked when the handler was an IntentService, except at boot time due to restrictions on background services. I'm therefore attempting to use a JobIntentService instead.

public class MyJobIntentService extends JobIntentService
{
    public int onStartCommand(@Nullable Intent intent, int flags, int startId)
    {
        // This gets called with the intent given to AlarmManager
        return super.onStartCommand(intent, flags, startId);
    }

    public static void enqueueWork(Context context, Intent work)
    {
        // Not called
        enqueueWork(context, NotificationService.class, JOB_ID, work);
    }

    public void onHandleWork(@NonNull Intent intent)
    {
        // Not called
        ...
    }

    // No other methods overridden
}

onStartCommand is called. The documentation says that this method:

Processes start commands when running as a pre-O service, enqueueing them to be later dispatched in onHandleWork(Intent).

I don't know what 'later' is supposed to mean here but onHandleWork is not in fact called, even though onStartCommand has been called with the expected intent.

I've read answers to similar questions and ensured that I don't override other methods. I've also created a manifest entry for the service which I assume is correct - or onStartCommand wouldn't be called.

This is how I create the PendingIntent used with AlarmManager:

Intent myIntent = new Intent( myContext, MyJobIntentService.class);
...
PendingIntent pendingIntent = PendingIntent.getService( mContext, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
myAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);

Any ideas on what I'm missing?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Timmy K
  • 291
  • 1
  • 2
  • 14
  • I’m unsure if the pending intent will actually start the JobIntentService as it never “Enqueues” work to be done, I’ve had the same problem and solved it with using the PendingIntent to open a BroadcastReceiver that was registered and that Receivers onReceive(Intent) method is used to launch the JobIntentService.enqueueWork(intent); – Brandon Dec 20 '18 at 17:12
  • Thanks. I still don't understand why it didn't work but I got it working according to your suggestion. For others facing the same issue: (1) I couldn't invoke the BroadcastReceiver unless I created an intent targeted specifically at it (via its class); (2) I found that I needed to call the static function enqueueWork(intent) explicitly as suggested, from the BroadcastReceiver, rather than trying to raise an intent for handling by the JobIntentService – Timmy K Dec 21 '18 at 15:50
  • @Brandon's suggestion doesn't work. I'm using the PendingIntent and the BroadcastReceiver doesn't get called. – Eduardo Xavier Feb 21 '19 at 19:57
  • Could you post me your code please @EduardoXavier – Brandon Feb 22 '19 at 00:57

1 Answers1

0

your can try this recommendation. Also, you may want to check your AndroidManifest.xml to android.permission.BIND_JOB_SERVICE permission.

binit92
  • 75
  • 15