Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android JobScheduler-如何跳过定期作业的第一次作业运行?_Android_Delay_Android Jobscheduler_Periodic Task - Fatal编程技术网

Android JobScheduler-如何跳过定期作业的第一次作业运行?

Android JobScheduler-如何跳过定期作业的第一次作业运行?,android,delay,android-jobscheduler,periodic-task,Android,Delay,Android Jobscheduler,Periodic Task,在我的应用程序中,我设置了一个定期作业,设置为每30分钟运行一次。 第一次作业运行正好发生在我安排定期作业时,这在我的情况下是不需要的。 我想要的是跳过第一次运行,这样它将在30多分钟后第一次运行 关于如何实现这一点,我的两个想法是,要么让它在最初的30分钟内完全不运行(某种延迟),要么在有机会开始之前将第一次作业运行标记为完成。 不幸的是,我在JobInfo中没有找到任何方法可以让我做这些事情 另一个可以满足我需求的解决方法是,以某种方式限制作业仅在应用程序处于后台时发生。它并不能完全解决这个

在我的应用程序中,我设置了一个定期作业,设置为每30分钟运行一次。 第一次作业运行正好发生在我安排定期作业时,这在我的情况下是不需要的。 我想要的是跳过第一次运行,这样它将在30多分钟后第一次运行

关于如何实现这一点,我的两个想法是,要么让它在最初的30分钟内完全不运行(某种延迟),要么在有机会开始之前将第一次作业运行标记为完成。 不幸的是,我在
JobInfo
中没有找到任何方法可以让我做这些事情

另一个可以满足我需求的解决方法是,以某种方式限制作业仅在应用程序处于后台时发生。它并不能完全解决这个问题,但在我的案例中,它可以作为一个解决办法

以下是安排定期作业的当前代码:

private void scheduleJob() {
    ComponentName componentName = new ComponentName(this, myRecurringTask.class);
    JobInfo info = new JobInfo.Builder(JOB_ID, componentName)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .setPersisted(true)
            .setPeriodic(1800000)
            .build();
    JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
    scheduler.schedule(info);
}

我希望有人遇到同样的情况,可以帮助我解决它。。。谢谢大家!

使用
WorkManager
安排后台工作,请参见简介

1。添加依赖项:

implementation "androidx.work:work-runtime-ktx:2.4.0"
class DataRefresher(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result {                 //will run on background thread
        //your logic

        return try {
            //your logic

            Result.success()
        } catch (e: HttpException) {
            Result.retry()
        }
    }
}
class DevBytesApplication : Application() {
    private val backgroundScope = CoroutineScope(Dispatchers.Default)       //standard background thread

    override fun onCreate() {                           //called when app launches, same as Activity
        super.onCreate()

        initWork()
    }

    private fun initWork() {
        backgroundScope.launch {                        //run in background, not affecting ui
            setupDataRefreshingWork()
        }
    }

    @SuppressLint("IdleBatteryChargingConstraints")
    private fun setupDataRefreshingWork() {
        val constraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.UNMETERED)          //when using wifi
            .setRequiresBatteryNotLow(true)
            .setRequiresCharging(true)
            .setRequiresDeviceIdle(true)                    //when not running heavy task
            .build()

        val repeatingRequest = PeriodicWorkRequestBuilder<DataRefresher>(1, TimeUnit.DAYS)      //【15 minutes is minimum!!】
            .setConstraints(constraints)
            .setInitialDelay(30, TimeUnit.MINUTES)        //【initial delay!!】
            .build()

        WorkManager.getInstance(this).enqueueUniquePeriodicWork(
            DataRefresher::class.java.simpleName,               //work name
            ExistingPeriodicWorkPolicy.KEEP,                    //if new work comes in with same name, discard it
            repeatingRequest
        )
    }
}
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.devbytestest">

    <application
        android:name=".DevBytesApplication"                 //【here, must!!!】
    
        ...

    </application>

</manifest>
2。创建工人类:

implementation "androidx.work:work-runtime-ktx:2.4.0"
class DataRefresher(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result {                 //will run on background thread
        //your logic

        return try {
            //your logic

            Result.success()
        } catch (e: HttpException) {
            Result.retry()
        }
    }
}
class DevBytesApplication : Application() {
    private val backgroundScope = CoroutineScope(Dispatchers.Default)       //standard background thread

    override fun onCreate() {                           //called when app launches, same as Activity
        super.onCreate()

        initWork()
    }

    private fun initWork() {
        backgroundScope.launch {                        //run in background, not affecting ui
            setupDataRefreshingWork()
        }
    }

    @SuppressLint("IdleBatteryChargingConstraints")
    private fun setupDataRefreshingWork() {
        val constraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.UNMETERED)          //when using wifi
            .setRequiresBatteryNotLow(true)
            .setRequiresCharging(true)
            .setRequiresDeviceIdle(true)                    //when not running heavy task
            .build()

        val repeatingRequest = PeriodicWorkRequestBuilder<DataRefresher>(1, TimeUnit.DAYS)      //【15 minutes is minimum!!】
            .setConstraints(constraints)
            .setInitialDelay(30, TimeUnit.MINUTES)        //【initial delay!!】
            .build()

        WorkManager.getInstance(this).enqueueUniquePeriodicWork(
            DataRefresher::class.java.simpleName,               //work name
            ExistingPeriodicWorkPolicy.KEEP,                    //if new work comes in with same name, discard it
            repeatingRequest
        )
    }
}
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.devbytestest">

    <application
        android:name=".DevBytesApplication"                 //【here, must!!!】
    
        ...

    </application>

</manifest>
3。创建应用程序类:

implementation "androidx.work:work-runtime-ktx:2.4.0"
class DataRefresher(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result {                 //will run on background thread
        //your logic

        return try {
            //your logic

            Result.success()
        } catch (e: HttpException) {
            Result.retry()
        }
    }
}
class DevBytesApplication : Application() {
    private val backgroundScope = CoroutineScope(Dispatchers.Default)       //standard background thread

    override fun onCreate() {                           //called when app launches, same as Activity
        super.onCreate()

        initWork()
    }

    private fun initWork() {
        backgroundScope.launch {                        //run in background, not affecting ui
            setupDataRefreshingWork()
        }
    }

    @SuppressLint("IdleBatteryChargingConstraints")
    private fun setupDataRefreshingWork() {
        val constraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.UNMETERED)          //when using wifi
            .setRequiresBatteryNotLow(true)
            .setRequiresCharging(true)
            .setRequiresDeviceIdle(true)                    //when not running heavy task
            .build()

        val repeatingRequest = PeriodicWorkRequestBuilder<DataRefresher>(1, TimeUnit.DAYS)      //【15 minutes is minimum!!】
            .setConstraints(constraints)
            .setInitialDelay(30, TimeUnit.MINUTES)        //【initial delay!!】
            .build()

        WorkManager.getInstance(this).enqueueUniquePeriodicWork(
            DataRefresher::class.java.simpleName,               //work name
            ExistingPeriodicWorkPolicy.KEEP,                    //if new work comes in with same name, discard it
            repeatingRequest
        )
    }
}
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.devbytestest">

    <application
        android:name=".DevBytesApplication"                 //【here, must!!!】
    
        ...

    </application>

</manifest>
class devbytes应用程序:Application(){
private val backgroundScope=CoroutineScope(Dispatchers.Default)//标准后台线程
覆盖应用程序启动时调用的fun onCreate(){//,与活动相同
super.onCreate()
初始化工作()
}
私人娱乐工作(){
backgroundScope.launch{//在后台运行,不影响ui
setupDataRefreshingWork()
}
}
@SuppressLint(“IdleBatteryChargingConstraints”)
私人娱乐设置数据刷新工作(){
val constraints=constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)//使用wifi时
.setRequiresBatteryNotLow(真)
.SETREQUIRESERCHARGING(真)
.setRequiresDeviceIdle(true)//不运行繁重任务时
.build()
val repeatingRequest=PeriodicWorkRequestBuilder(1,TimeUnit.DAYS)/【至少15分钟!!】
.setConstraints(约束)
.setInitialDelay(30,时间单位.分钟)/[初始延迟!!]
.build()
WorkManager.getInstance(this.enqueueUniquePeriodicWork)(
DataRefresh::class.java.simpleName,//工作名称
ExistingPeriodicWorkPolicy.KEEP,//如果新工作以相同的名称进入,则放弃它
重复请求
)
}
}
4。设置AndroidManifest:

implementation "androidx.work:work-runtime-ktx:2.4.0"
class DataRefresher(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result {                 //will run on background thread
        //your logic

        return try {
            //your logic

            Result.success()
        } catch (e: HttpException) {
            Result.retry()
        }
    }
}
class DevBytesApplication : Application() {
    private val backgroundScope = CoroutineScope(Dispatchers.Default)       //standard background thread

    override fun onCreate() {                           //called when app launches, same as Activity
        super.onCreate()

        initWork()
    }

    private fun initWork() {
        backgroundScope.launch {                        //run in background, not affecting ui
            setupDataRefreshingWork()
        }
    }

    @SuppressLint("IdleBatteryChargingConstraints")
    private fun setupDataRefreshingWork() {
        val constraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.UNMETERED)          //when using wifi
            .setRequiresBatteryNotLow(true)
            .setRequiresCharging(true)
            .setRequiresDeviceIdle(true)                    //when not running heavy task
            .build()

        val repeatingRequest = PeriodicWorkRequestBuilder<DataRefresher>(1, TimeUnit.DAYS)      //【15 minutes is minimum!!】
            .setConstraints(constraints)
            .setInitialDelay(30, TimeUnit.MINUTES)        //【initial delay!!】
            .build()

        WorkManager.getInstance(this).enqueueUniquePeriodicWork(
            DataRefresher::class.java.simpleName,               //work name
            ExistingPeriodicWorkPolicy.KEEP,                    //if new work comes in with same name, discard it
            repeatingRequest
        )
    }
}
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.devbytestest">

    <application
        android:name=".DevBytesApplication"                 //【here, must!!!】
    
        ...

    </application>

</manifest>


初始延迟可在
WorkManager
中获得:感谢您的回复@SamChen。我以前从未听说过
WorkManager
,我只是检查了它的功能集,与
JobScheduler
相比,它确实带来了很多改进,同时仍然向后兼容。如果我在
JobScheduler
中找不到任何解决方案,我将迁移到
WorkManager
,我肯定会在新应用中使用它!谢谢你的回答。我肯定会考虑迁移到<代码> WorkMase< /Cord>,因为它完全有我自己需要的选项。目前,我通过跟踪作业在SharedReferences中运行的时间,并在第一次运行时跳过作业操作,实现了一种变通方法。