Skip to content

Commit

Permalink
부팅시 서비스 실행하기
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee-mtcom committed Jun 30, 2020
1 parent d1d257d commit 20492a5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package="com.junsu.sample">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:name=".App"
Expand All @@ -22,6 +24,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.BootService"
android:enabled="true">
</service>
<receiver android:name=".receiver.BootReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>

</manifest>
2 changes: 2 additions & 0 deletions app/src/main/java/com/junsu/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity

import kotlinx.android.synthetic.main.activity_main.*
import com.junsu.sample.R
import com.junsu.sample.service.BootService
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -16,6 +17,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)

fab.setOnClickListener { view ->
BootService.start(applicationContext)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/junsu/sample/receiver/BootReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.junsu.sample.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.junsu.sample.service.BootService
import timber.log.Timber

class BootReceiver: BroadcastReceiver() {
override fun onReceive(c: Context?, i: Intent?) {
Timber.d("!!!action: ${i?.action}")
when (i?.action) {
Intent.ACTION_BOOT_COMPLETED -> {
c?.also {
BootService.start(it)
}
}
}
}
}
80 changes: 80 additions & 0 deletions app/src/main/java/com/junsu/sample/service/BootService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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 timber.log.Timber


class BootService : IntentService(TAG) {
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
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
}
}
}

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}")
when (intent?.action) {
ACTION_BOOT_SERVICE -> {
Thread.sleep(5000)
Timber.d("handling boot service action")
}
}
}
}

0 comments on commit 20492a5

Please sign in to comment.