davidliu
Committed by GitHub

Foreground service example to keep call running in background (#175)

... ... @@ -12,4 +12,8 @@
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<application>
<service android:name="io.livekit.android.sample.service.ForegroundService" />
</application>
</manifest>
\ No newline at end of file
... ...
... ... @@ -3,6 +3,7 @@ package io.livekit.android.sample
import android.app.Application
import android.content.Intent
import android.media.projection.MediaProjectionManager
import android.os.Build
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
... ... @@ -22,6 +23,7 @@ import io.livekit.android.room.track.CameraPosition
import io.livekit.android.room.track.LocalScreencastVideoTrack
import io.livekit.android.room.track.LocalVideoTrack
import io.livekit.android.room.track.Track
import io.livekit.android.sample.service.ForegroundService
import io.livekit.android.util.flow
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
... ... @@ -119,6 +121,16 @@ class CallViewModel(
}
connectToRoom()
}
// Start a foreground service to keep the call from being interrupted if the
// app goes into the background.
val foregroundServiceIntent = Intent(application, ForegroundService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
application.startForegroundService(foregroundServiceIntent)
} else {
application.startService(foregroundServiceIntent)
}
}
private suspend fun connectToRoom() {
... ... @@ -216,6 +228,11 @@ class CallViewModel(
super.onCleared()
room.disconnect()
room.release()
// Clean up foreground service
val application = getApplication<Application>()
val foregroundServiceIntent = Intent(application, ForegroundService::class.java)
application.stopService(foregroundServiceIntent)
}
fun setMicEnabled(enabled: Boolean) {
... ...
package io.livekit.android.sample.service
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
/**
* A foreground service is required for screen capture on API level Q (29) and up.
* This a simple default foreground service to display a notification while screen
* capturing.
*/
open class ForegroundService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
}
val actualNotification =
NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
startForeground(DEFAULT_NOTIFICATION_ID, actualNotification)
return START_NOT_STICKY
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel() {
val channel = NotificationChannel(
DEFAULT_CHANNEL_ID,
"Foreground",
NotificationManager.IMPORTANCE_LOW
)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
override fun onBind(intent: Intent?): IBinder? {
return null // not used.
}
companion object {
const val DEFAULT_NOTIFICATION_ID = 3456
const val DEFAULT_CHANNEL_ID = "livekit_example_foreground"
}
}
\ No newline at end of file
... ...