Skip to content

Instantly share code, notes, and snippets.

{
"a":55,
"b":"rotem matityahu",
"c":30.0,
"d":true
}
class Settings(
val volume: Int,
val userName: String,
val fontSize: Float,
val privacyEnabled: Boolean
)
@rotman
rotman / ReposForegroundService.kt
Last active February 23, 2021 15:26
Create your foreground task
class ReposForegroundService : ForegroundTaskService() {
override fun getNotification(): Notification {
// create channel
val channelId = resources.getString(R.string.my_channel)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT)
notificationChannel.setSound(null, null)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
@rotman
rotman / ScheduleForegroundTask.kt
Last active February 23, 2021 15:27
Create and schedule foreground task
fun scheduleForegroundTask() {
val foregroundTaskInfo = foregroundTaskInfo {
id = TASK_ID
networkType = NetworkType.Any
persisted = true
minLatencyMillis = TimeUnit.HOURS.toMillis(12)
timeoutMillis = TimeUnit.MINUTES.toMillis(1)
retryData = RetryData(retryPolicy = RetryPolicy.Exponential, initialBackoff = 4000)
}
ForegroundTasksSchedulerWrapper().scheduleForegroundTask(
class MyWorker(context: Context, parameters: WorkerParameters) :
Worker(context, parameters) {
override fun doWork(): Result {
setForeground(createForegroundInfo())
downloadSomething()
return Result.success()
}
private fun createForegroundInfo(): ForegroundInfo {