JobSchedule периодически не работает.Вот файл манифеста.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receiver.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".JobScheduleService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"/>
</application>
Затем в MainActivity.kt я инициализировал и запланировал задание
val jobScheduler = applicationContext
.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
val componentName = ComponentName(this,
JobScheduleService::class.java!!)
val jobInfoObj = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) Builder(777, componentName)
// create a job that stays as scheduled even after device reboots,
// you need call setPersisted() method by passing true as value to it
.setPersisted(true)
// create a job that needs to be rerun if it fails,
// you need to use setBackOffCriteria() method passing time interval
// for the first time retry and retry policy which is used to calculate
// time interval for retries after first retry.
.setBackoffCriteria(TimeUnit.MINUTES.toMillis(2000), BACKOFF_POLICY_LINEAR)
.setPeriodic(TimeUnit.MINUTES.toMillis(2000), TimeUnit.MINUTES.toMillis(3000))
.build() else {
TODO("VERSION.SDK_INT < N")
}
jobScheduler.schedule(jobInfoObj);
А вот класс JobService:
class JobScheduleService:JobService(){
override fun onStartJob(params: JobParameters?): Boolean {
Log.d("1", "onStartJob")
sendMessage(1, params = params)
return true
}
override fun onStopJob(params: JobParameters?): Boolean {
Log.d("1", "onStopJob")
sendMessage(2, params = params)
return true
}
private fun sendMessage(messageID: Int, params: JobParameters?) {
// If this service is launched by the JobScheduler, there's no callback Messenger. It
// only exists when the MainActivity calls startService() with the callback in the Intent.
try {
Timer().schedule(timerTask {
Log.e(messageID.toString(), "job finished")
jobFinished(params, true)
}, 2000)
Log.i(messageID.toString(), "sending message on schedule")
} catch (e: RemoteException) {
Log.e(messageID.toString(), "Error passing service object back to activity.")
}
finally {
}
}
}
Когда я запускаю сборку, она регистрируется под сообщением, а затем никогда не вызывается метод onStart / onStop.
09-20 14:02:48.330 9417-9417/com.thakur.android.jobscheduler W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
09-20 14:02:48.438 9417-9417/com.thakur.android.jobscheduler D/1: onStartJob
09-20 14:02:48.439 9417-9417/com.thakur.android.jobscheduler I/1: sending message on schedule
09-20 14:02:50.440 9417-9568/com.thakur.android.jobscheduler E/1: job finished
09-20 14:02:53.201 9417-9500/com.thakur.android.jobscheduler I/pending: 1
09-20 14:02:58.202 9417-9500/com.thakur.android.jobscheduler I/pending: 1
09-20 14:03:03.204 9417-9500/com.thakur.android.jobscheduler I/pending: 1
...
09-20 14:16:53.269 9417-9500/com.thakur.android.jobscheduler I/pending: 1
09-20 14:16:58.270 9417-9500/com.thakur.android.jobscheduler I/pending: 1
09-20 14:17:03.270 9417-9500/com.thakur.android.jobscheduler I/pending: 1