JobIntentService 无法在 Oreo (API <= 26) 以下的 API 上工作

JobIntentService not working on API below Oreo (API <= 26)

我正在为 Android 设置一项新服务,这是我第一次使用 JobIntentService。该服务还包括一个通知,当我 运行 该应用程序的 Android 版本低于 Oreo 时,该通知不会显示。工作本身一直工作到最后。

这是我试过的代码,它非常适合 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());

我的 gradle 文件如下所示:

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

我还在 gradle 文件中添加了这个库:

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

清单文件中的权限:

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

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

我在控制台中收到此警告。

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)

我认为你必须通过广播接收器来处理这个问题,在 28 之前直接使用 Apis 的工作意图服务会产生性能开销。

顺便说一句,这个演示将帮助您解决问题。 https://github.com/Ahabdelhak/Service_AlarmManager_Broadcast_Notification