Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
xuning
/
livekitAndroidXuningTest
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
davidliu
2023-01-19 21:10:59 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
GitHub
2023-01-19 21:10:59 +0900
Commit
b6ff176c9b14e947ab38a5fe6c5f48132ea657d8
b6ff176c
1 parent
4a0035ac
Foreground service example to keep call running in background (#175)
隐藏空白字符变更
内嵌
并排对比
正在显示
3 个修改的文件
包含
75 行增加
和
0 行删除
sample-app-common/src/main/AndroidManifest.xml
sample-app-common/src/main/java/io/livekit/android/sample/CallViewModel.kt
sample-app-common/src/main/java/io/livekit/android/sample/service/ForegroundService.kt
sample-app-common/src/main/AndroidManifest.xml
查看文件 @
b6ff176
...
...
@@ -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
...
...
sample-app-common/src/main/java/io/livekit/android/sample/CallViewModel.kt
查看文件 @
b6ff176
...
...
@@ -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) {
...
...
sample-app-common/src/main/java/io/livekit/android/sample/service/ForegroundService.kt
0 → 100644
查看文件 @
b6ff176
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
...
...
请
注册
或
登录
后发表评论