Skip to content

Commit

Permalink
IntentService -> JobIntentService
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee-mtcom committed Jul 1, 2020
1 parent 20492a5 commit 4dfe333
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 50 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
android:name=".App"
Expand Down
54 changes: 4 additions & 50 deletions app/src/main/java/com/junsu/sample/service/BootService.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package com.junsu.sample.service

import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.junsu.sample.R
import androidx.core.app.JobIntentService
import timber.log.Timber


class BootService : IntentService(TAG) {
class BootService : JobIntentService() {
companion object {
private const val TAG = "BootService"
private const val ACTION_BOOT_SERVICE = "com.junsu.sample.service.actionBootService"
private const val NOTIFICATION_CHANNEL_ID = "com.junsu.sample"
private const val NOTIFICATION_CHANNEL_NAME = "BootService"
private const val SERVICE_CHANNEL_ID = 3000
fun start(context: Context) {
Intent(context, BootService::class.java).also { intent ->
intent.action = ACTION_BOOT_SERVICE
Expand All @@ -30,46 +22,8 @@ class BootService : IntentService(TAG) {
}
}

override fun onCreate() {
makeNotification()
super.onCreate()
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(
channelId: String,
channelName: String,
f: (NotificationChannel) -> Unit = {}
): String {
val channel =
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE)
f(channel)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
return channelId
}

private fun makeNotification() {
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME)
} else {
""
}

val builder = NotificationCompat.Builder(this, channelId)
.setContentTitle(getString(R.string.app_name))
.setContentText("running...")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
val notification = builder.build()

startForeground(SERVICE_CHANNEL_ID, notification)
}

override fun onHandleIntent(intent: Intent?) {
Timber.d("${intent?.action}")
override fun onHandleWork(intent: Intent) {
Timber.d("${intent.action}")
when (intent?.action) {
ACTION_BOOT_SERVICE -> {
Thread.sleep(5000)
Expand Down

0 comments on commit 4dfe333

Please sign in to comment.