diff --git a/src/androidTest/AndroidManifest.xml b/src/androidTest/AndroidManifest.xml
index 259b616e..582076c4 100644
--- a/src/androidTest/AndroidManifest.xml
+++ b/src/androidTest/AndroidManifest.xml
@@ -3,5 +3,7 @@
+
diff --git a/src/androidTest/java/androidx/core/content/ContextTest.kt b/src/androidTest/java/androidx/core/content/ContextTest.kt
index 09d2355f..4ada37df 100644
--- a/src/androidTest/java/androidx/core/content/ContextTest.kt
+++ b/src/androidTest/java/androidx/core/content/ContextTest.kt
@@ -16,15 +16,20 @@
package androidx.core.content
+import android.app.job.JobParameters
+import android.app.job.JobScheduler
+import android.app.job.JobService
import android.content.ContextWrapper
import android.support.test.InstrumentationRegistry
import android.support.test.filters.SdkSuppress
-import androidx.core.ktx.test.R
import androidx.core.getAttributeSet
+import androidx.core.ktx.test.R
import org.junit.Assert.assertEquals
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
+import java.util.concurrent.CountDownLatch
+import java.util.concurrent.TimeUnit.MILLISECONDS
class ContextTest {
private val context = InstrumentationRegistry.getContext()
@@ -70,4 +75,31 @@ class ContextTest {
assertTrue(getInt(R.styleable.SampleAttrs_sample, -1) != -1)
}
}
+
+ @Test fun testScheduleJob() {
+ val result = context.scheduleJob(0) {
+ setOverrideDeadline(1)
+ }
+
+ latch.await(20, MILLISECONDS)
+
+ assertTrue(called)
+ assertEquals(JobScheduler.RESULT_SUCCESS, result)
+ }
+
+ companion object {
+ private val latch = CountDownLatch(1)
+ private var called = false
+
+ class TestService : JobService() {
+ override fun onStartJob(params: JobParameters): Boolean {
+ called = true
+ latch.countDown()
+ jobFinished(params, false)
+ return true
+ }
+
+ override fun onStopJob(params: JobParameters): Boolean = false
+ }
+ }
}
diff --git a/src/main/java/androidx/core/content/Context.kt b/src/main/java/androidx/core/content/Context.kt
index 2ffb17b7..6cac5b7f 100644
--- a/src/main/java/androidx/core/content/Context.kt
+++ b/src/main/java/androidx/core/content/Context.kt
@@ -16,6 +16,10 @@
package androidx.core.content
+import android.app.Service
+import android.app.job.JobInfo
+import android.app.job.JobScheduler
+import android.content.ComponentName
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
@@ -91,3 +95,21 @@ inline fun Context.withStyledAttributes(
typedArray.recycle()
}
}
+
+/**
+ * Schedules a job with the Service [T].
+ *
+ * @param jobId the id of the job being scheduled
+ * @param buildSequence a lambda to set the appropriate JobInfo.Builder params
+ */
+@RequiresApi(21)
+inline fun Context.scheduleJob(
+ jobId: Int,
+ buildSequence: JobInfo.Builder.() -> Unit
+): Int {
+ val info = JobInfo.Builder(jobId, ComponentName(this, T::class.java))
+ .apply(buildSequence)
+ .build()
+ val scheduler = this.getSystemService(Context.JOB_SCHEDULER_SERVICE) as? JobScheduler
+ return scheduler?.schedule(info) ?: JobScheduler.RESULT_FAILURE
+}