Skip to content

Commit

Permalink
[example] Fix crash when run on Android 14 (#1967)
Browse files Browse the repository at this point in the history
Adapt foreground service changes on Android 14.


https://developer.android.com/about/versions/14/changes/fgs-types-required

Fix #1783
  • Loading branch information
littleGnAl authored Aug 21, 2024
1 parent e1a2f62 commit a1d390d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
package="io.agora.agora_rtc_ng_example">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
android:label="agora_rtc_ng_example"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Binder
import android.os.Build
import android.os.Bundle
Expand Down Expand Up @@ -89,7 +90,14 @@ class ExampleService: Service() {
.setContentIntent(getContentIntent())
.setShowWhen(false)

startForeground(NOTIFICATION_ID_DAEMON_SERVICE, notificationBuilder.build())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
startForeground(
NOTIFICATION_ID_DAEMON_SERVICE,
notificationBuilder.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE xor ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
} else {
startForeground(NOTIFICATION_ID_DAEMON_SERVICE, notificationBuilder.build())
}
}

private fun getContentIntent(): PendingIntent? {
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class _MyAppState extends State<MyApp> {

Future<void> _requestPermissionIfNeed() async {
if (defaultTargetPlatform == TargetPlatform.android) {
await [Permission.microphone, Permission.camera].request();
await [Permission.audio, Permission.microphone, Permission.camera].request();
}
}

Expand Down

0 comments on commit a1d390d

Please sign in to comment.