在 Android 8+ 上停止应用程序时会终止定期任务

Con*_* N. 1 android alarmmanager periodic-task android-jobscheduler android-workmanager

大家好,我正在尝试在 Android 上实现定期任务,但我被困在某些设备上。

我需要每 15 或 30 分钟在后台运行一次任务。这在 Android 8.0 之前的版本上运行良好。但在 8+ 上,它仅在应用程序处于后台或前台时才有效。当应用程序从最近的应用程序中滑出时,计划任务在真实设备(Ulefone note 7(Android 8.1)、Tecno LC7(Android 10)、itel A56(Abdroid 9))上被终止,但在模拟器(Android 10)上运行良好。我尝试了几种方法:

1.Workmanager(仅当应用程序处于后台或前台时才工作)

构建.gradle

implementation "androidx.work:work-runtime:2.4.0"
Run Code Online (Sandbox Code Playgroud)

主要活动

PeriodicWorkRequest periodicSyncDataWork =
                    new PeriodicWorkRequest.Builder(NotificationWorker.class, 15,TimeUnit.MINUTES)
                            .addTag("TAG_SYNC_DATA")
                            .setBackoffCriteria(BackoffPolicy.LINEAR,PeriodicWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)
                            .build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork(
                    "NOTIFICATION_WORKER",
                    ExistingPeriodicWorkPolicy.REPLACE, //Existing Periodic Work policy
                     periodicSyncDataWork //work request
            );
Run Code Online (Sandbox Code Playgroud)

通知工作者

public class NotificationWorker extends Worker {
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) 
{
    super(context, workerParams);
}


@NonNull
@Override
public Result doWork() {
    Log.d("MYWORKER", "LLLLLLLLLLL");
    //My code here
    return Result.success();
}
Run Code Online (Sandbox Code Playgroud)

}

2.JobScheduler(仅当应用程序处于后台或前台时才有效)

ComponentName serviceComponent = new ComponentName(context, NotifJobService.class);
    JobInfo.Builder builder = new JobInfo.Builder(1880, serviceComponent);
    builder.setPersisted(true);
    builder.setPeriodic(16*60*1000, 20*60 *1000);
    JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
    jobScheduler.schedule(builder.build());
Run Code Online (Sandbox Code Playgroud)

3.警报管理器(不触发广播接收器)

主要代码

Intent intent = new Intent(getApplicationContext(), MyIntentService.class);
    final PendingIntent pIntent = PendingIntent.getBroadcast(this, 100,intent, PendingIntent.FLAG_UPDATE_CURRENT);
    long firstMillis = System.currentTimeMillis();
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent);
Run Code Online (Sandbox Code Playgroud)

广播接收器

public class NotificationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Intent in = new Intent(context, MyIntentService.class);
    context.startService(in);
}
}
Run Code Online (Sandbox Code Playgroud)

意图服务

public class MyIntentService extends IntentService {
    public MyIntentService(String name) {
        super(name);
    }
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d("NotifIntentService", "Starting");
        //My task here
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白我在这里做错了什么。请帮忙

Kei*_*n.k 5

我已经被同一问题困扰好几天了,最终发现还没有正确的方法来做到这一点,只能要求用户授予一些权限(自动启动、电池保护程序优化等......)

您可以在这里找到更多信息: https: //dontkillmyapp.com

这个问题有很多不同的方式提出,没有好的答案,但在这里你可能会找到一些抽象的答案,这就是我刚才告诉你的。 小米和oppo等中国ROM上的工作管理器,在电池优化时,将工作的计划延迟增加几个小时