gpt4 book ai didi

android - 当应用程序被杀死时,Job Scheduler 服务不工作

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:20 31 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,它需要每小时执行一个后台任务(作业计划程序或服务)。应用程序运行时会执行任务,但一旦我从前台终止应用程序,服务就无法正常工作。还有另一种方法可以实现这一目标吗?

<强>1。服务

public class NotificationService extends JobService {

private void PrintLog()
{
Log.d("DGVCL", "PrintLog()");
}

@Override
public boolean onStartJob(JobParameters jobParameters) {
Log.d("DGVCL", "onStartJob()");
PrintLog();
jobFinished(jobParameters, false);
return true;
}

@Override
public boolean onStopJob(JobParameters jobParameters) {
Log.d("DGVCL", "onStopJob()");
return true;
}
}

<强>2。主要 Activity

JobScheduler jobScheduler = (JobScheduler)getApplicationContext().getSystemService(JOB_SCHEDULER_SERVICE);
ComponentName componentName = new ComponentName(this, NotificationService.class);
JobInfo jobInfo = new JobInfo.Builder(1, componentName)
.setPeriodic(Global.NOTIFICATION_TIME_PERIOD)
.setBackoffCriteria(Global.NOTIFICATION_TIME_PERIOD, JobInfo.BACKOFF_POLICY_LINEAR)
.setPersisted(true).build();
jobScheduler.schedule(jobInfo);

<强>3。 list

<service android:name="com.hopesndreams.hiren.hd.service.NotificationService" android:permission="android.permission.BIND_JOB_SERVICE" >
</service>

最佳答案

使用 WorkManager,它构建在 JobScheduler 之上,专门用于承担所有后台服务,包括前台和后台功能。 https://developer.android.com/topic/libraries/architecture/workmanager

关于android - 当应用程序被杀死时,Job Scheduler 服务不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55118369/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com