포그라운드 서비스 시작시 foregroundServiceTypes 추가

This commit is contained in:
klaus 2024-08-26 15:24:45 +09:00
parent adab4aee48
commit 38864ca666
3 changed files with 37 additions and 3 deletions

View File

@ -80,6 +80,17 @@
<option name="screenX" value="960" />
<option name="screenY" value="2142" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="comet" />
<option name="id" value="comet" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro Fold" />
<option name="screenDensity" value="390" />
<option name="screenX" value="2076" />
<option name="screenY" value="2152" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="29" />
<option name="brand" value="samsung" />

View File

@ -6,10 +6,10 @@ import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Build
import android.os.Handler
@ -511,7 +511,16 @@ class AudioContentPlayService :
.setShowActionsInCompactView(0, 1)
)
startForeground(1, notificationBuilder.build())
val notification = notificationBuilder.build()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(
1,
notification,
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
)
} else {
startForeground(1, notification)
}
}
override fun onLoadCleared(placeholder: Drawable?) {

View File

@ -7,6 +7,8 @@ import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
@ -33,7 +35,19 @@ class SodaLiveService : Service() {
}
private fun updateNotification(content: String) {
startForeground(Constants.LIVE_SERVICE_NOTIFICATION_ID, createNotification(content))
val notification = createNotification(content)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val foregroundServiceTypes = FOREGROUND_SERVICE_TYPE_MICROPHONE or
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
startForeground(
Constants.LIVE_SERVICE_NOTIFICATION_ID,
notification,
foregroundServiceTypes
)
} else {
startForeground(Constants.LIVE_SERVICE_NOTIFICATION_ID, notification)
}
}
private fun createNotification(content: String): Notification {