Ad

JobIntentService Not Working On API Below Oreo (API <= 26)

I'm setting up a new service for Android and I'm using JobIntentService for the first time. The service includes also a notification which is not displayed when I'm running the app in an Android version below Oreo. The job itself works until the end.

This is the code I've tried that works perfectly fine for API >= 26.

private void initializeNotification (String fileName) {

        intent = new Intent(this, UploadActivity.class);
        intent.putExtra("file_name", fileName);
        intent.putExtra("file_list", (Serializable) auxList);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        long[] vibratePattern = {0L};
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel notificationChannel = new NotificationChannel(MyConstants.NOTIFICATION_CHANNEL_ID, "Upload", NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setVibrationPattern(vibratePattern);
            notificationChannel.enableVibration(true);
            notificationManager = getSystemService(NotificationManager.class);
        }
        notification = new NotificationCompat.Builder(this, MyConstants.NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.icon)
                .setColor(getResources().getColor(R.color.primary))
                .setContentText(fileName)
                .setPriority(PRIORITY_DEFAULT)
                .setProgress(PROGRESSBAR_MAX, 0, false)
                .setContentIntent(pendingIntent)
                .setVibrate(vibratePattern)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(fileName))
                .setAutoCancel(false);
        notificationManager.notify(NOTIFICATION_ID, notification.build());

My gradle file looks like this:

compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "es.app.services"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 12
        versionName "1"
        // Enabling multidex support.
        multiDexEnabled true

I've also added this library in the gradle file:

implementation "com.android.support:support-compat:28.0.0"

And this permissions in the manifest file:

<uses-permission android:name="android.permission.WAKE_LOCK" />

<service android:name=".services.UploadJobIntentService"
            android:permission="android.permission.BIND_JOB_SERVICE"/>

I'm getting this warning in the console.

Rejecting re-init on previously-failed class java.lang.Class<androidx.core.app.JobIntentService$JobServiceEngineImpl>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/app/job/JobServiceEngine;
    at void es.app.services.UploadJobIntentService.enqueueWork(android.content.Context, android.content.Intent) (UploadJobIntentService.java:-1)
   ...
    at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
    at void android.view.View$DeclaredOnClickListener.onClick(android.view.View) (View.java:4727)
    at boolean android.view.View.performClick() (View.java:5647)
    at void android.view.View$PerformClick.run() (View.java:22479)
    at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:761)
    at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:98)
    at void android.os.Looper.loop() (Looper.java:156)
    at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6595)
    at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
    at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:942)
    at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:832)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.app.job.JobServiceEngine" on path: DexPathList[[zip file "/data/app/es.app-1/base.apk", zip file "/data/app/es.app-1/split_lib_dependencies_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_0_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_1_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_2_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_3_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_4_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_5_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_6_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_7_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_8_apk.apk", zip file "/data/app/es.app-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/es.app-1/lib/arm64, /data/app/es.app-1/base.apk!/lib/arm64-v8a, /data
    at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
    at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
    at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
    ...
    at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
    at void android.view.View$DeclaredOnClickListener.onClick(android.view.View) (View.java:4727)
    at boolean android.view.View.performClick() (View.java:5647)
    at void android.view.View$PerformClick.run() (View.java:22479)
    at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:761)
    at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:98)
    at void android.os.Looper.loop() (Looper.java:156)
    at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6595)
    at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
    at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:942)
    at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:832)
Ad

Answer

I think you have to handle this by broadcast receiver, using job Intent service directly with Apis before 28 make performance overhead.

By the way this Demo will help you in your issue. https://github.com/Ahabdelhak/Service_AlarmManager_Broadcast_Notification

Ad
source: stackoverflow.com
Ad