Search code examples
androidintentserviceandroid-jobscheduler

JobIntentService not calling onHandleWork


I'm new to Services and decided to use a JobIntentService because my app has to support API 21+ but when I start my service on API 28+ it's not working properly.

I've found out that onHandleWork isn't called when I start my service with Contextcompat.startForegroundService()

My code:

My Activity

Intent intent = new Intent(this, MyService.class);
        intent.putExtra("fileTitle", popup.name);
ContextCompat.startForegroundService(this,intent);

My Service

@Override
    public void onCreate() {
        super.onCreate();

        MyLog(this, "SERVICE ONCREATE", "SERVICE");
        createNotificationChannel();
        initNotification();
        startForeground(NOTIFICATION_ID, nBuilder.build());
    }

Solution

  • To close this subject, this is how I manage to make it work:

    My Activity

    Intent intent = new Intent(this, MyService.class);
    intent.putExtra("fileTitle", popup.name);
    ContextCompat.startForegroundService(this,intent);
    

    My Service

    @Override
    public void onCreate() {
        super.onCreate();
        // Init for Notification Channel in API 26+ && init notification to display
        createNotificationChannel();
        initNotification();
    }
    
    
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if(intent!= null && isPostorEqualOS(Build.VERSION_CODES.O)){
            startForeground(NOTIFICATION_ID, nBuilder.build());
        }
    }