Android(Kotlin)

Simple ForegroundService

----___<<<<< 2022. 10. 15. 14:14

 

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<service android:name=".MyForegroundService"/>

 

class MyForegroundService : Service() {

    private val TAG = MyForegroundService::class.java.simpleName
    private val NOTIFICATION_ID = 10
    
    override fun onCreate() {
        super.onCreate()

        Log.d(TAG, "onCreate")


    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        Log.d(TAG, "onConStartCommandreate")
        Log.d(TAG, intent?.action.toString())

        val channelId = "com.codechacha.sample1"
        val channelName = "My service channel"
        if (Build.VERSION.SDK_INT >= 26) {
            val channel = NotificationChannel(
                channelId, channelName,
                NotificationManager.IMPORTANCE_DEFAULT
            )
            var manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(channel)
        }

        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("title")
            .setContentText("content text")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_CALL)


        startForeground(NOTIFICATION_ID, makeNotification())


        return START_STICKY
    }

    override fun onBind(p0: Intent?): IBinder? {

        return null

    }

    fun makeNotification() : Notification {


        val random = Random()
        val num = random.nextInt(5)

        var builder = NotificationCompat.Builder(this, "CHANNEL_ID")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("My notification")
            .setContentText(num.toString())
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = "name"
            val descriptionText = "channel_description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel("CHANNEL_ID", name, importance).apply {
                description = descriptionText
            }

            val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }

        return builder.build()

    }


    fun notitest(){

        val random = Random()
        val num = random.nextInt(5)

        var builder = NotificationCompat.Builder(this, "CHANNEL_ID")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("My notification")
            .setContentText(num.toString())
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = "name"
            val descriptionText = "channel_description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel("CHANNEL_ID", name, importance).apply {
                description = descriptionText
            }

            val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)

            notificationManager.notify(NOTIFICATION_ID, builder.build())

        }

    }


}

 

'Android(Kotlin)' 카테고리의 다른 글

Android ViewModel Context  (0) 2022.10.20
Android BOOT_COMPLETED  (0) 2022.10.20
Android ROOM sqlcipher  (0) 2022.09.20
Android AlarmManager  (0) 2022.09.16
bottomNavigationView  (0) 2022.08.30