-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
173 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.umc_6th | ||
|
||
object Constant { | ||
const val CHANNEL_ID = "ch123" | ||
const val MUSIC_NOTIFICATION_ID = 123 | ||
} |
62 changes: 62 additions & 0 deletions
62
UMC_6th/app/src/main/java/com/example/umc_6th/ForegroundService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.example.umc_6th | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.app.Service | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.os.IBinder | ||
import androidx.annotation.RequiresApi | ||
|
||
class ForegroundService : Service() { | ||
|
||
override fun onBind(intent: Intent): IBinder? { | ||
return null | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
createNotificationChannel() | ||
} | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 버전 확인 | ||
showNotification() | ||
} | ||
|
||
return START_STICKY | ||
} | ||
@RequiresApi(Build.VERSION_CODES.O) | ||
private fun showNotification() { | ||
val notificationIntent = Intent(this, SongActivity::class.java) | ||
val pendingIntent = PendingIntent.getActivity( | ||
this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE | ||
) | ||
val notification = Notification | ||
.Builder(this, Constant.CHANNEL_ID) | ||
.setContentText("현재 음악이 재생 중입니다.") | ||
.setSmallIcon(R.drawable.ic_flo_logo) | ||
.setContentIntent(pendingIntent) | ||
.build() | ||
|
||
startForeground(Constant.MUSIC_NOTIFICATION_ID, notification) | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val serviceChannel = NotificationChannel( | ||
Constant.CHANNEL_ID, "Service Channel", | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
) | ||
|
||
val manager = getSystemService( | ||
NotificationManager::class.java | ||
) | ||
|
||
manager.createNotificationChannel(serviceChannel) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters