[Android] Fix ForegroundServiceStartNotAllowedException for the DebugLogService
Nicolas Pomepuy
git at videolan.org
Tue Jul 7 12:23:37 UTC 2026
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Jun 17 08:04:40 2026 +0200| [ec0abb6ac1c3fa319e5c45eabe643bf65e39024d] | committer: Nicolas Pomepuy
Fix ForegroundServiceStartNotAllowedException for the DebugLogService
> https://code.videolan.org/videolan/vlc-android/commit/ec0abb6ac1c3fa319e5c45eabe643bf65e39024d
---
application/vlc-android/AndroidManifest.xml | 7 ++++++-
.../src/org/videolan/vlc/DebugLogService.kt | 20 +++++++++++++++++++-
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/application/vlc-android/AndroidManifest.xml b/application/vlc-android/AndroidManifest.xml
index f23beb5407..4f89a1b1cf 100644
--- a/application/vlc-android/AndroidManifest.xml
+++ b/application/vlc-android/AndroidManifest.xml
@@ -20,6 +20,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <!-- normal -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
@@ -1204,7 +1205,11 @@
android:launchMode="singleTop" />
<service android:name=".DebugLogService"
android:process=":logger"
- android:foregroundServiceType="dataSync" />
+ android:foregroundServiceType="specialUse">
+ <property
+ android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
+ android:value="Collecting logs for debugging purposes" />
+ </service>
</application>
</manifest>
diff --git a/application/vlc-android/src/org/videolan/vlc/DebugLogService.kt b/application/vlc-android/src/org/videolan/vlc/DebugLogService.kt
index 5460a3bba8..3545447550 100644
--- a/application/vlc-android/src/org/videolan/vlc/DebugLogService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/DebugLogService.kt
@@ -21,6 +21,7 @@
package org.videolan.vlc
import android.annotation.TargetApi
+import android.app.ForegroundServiceStartNotAllowedException
import android.app.PendingIntent
import android.app.Service
import android.content.ComponentName
@@ -35,6 +36,7 @@ import android.os.Looper
import android.os.RemoteCallbackList
import android.os.RemoteException
import android.text.format.DateFormat
+import android.util.Log
import androidx.core.app.NotificationCompat
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.resources.AndroidDevices
@@ -65,6 +67,11 @@ class DebugLogService : Service(), Logcat.Callback, Runnable {
private val callbacks = RemoteCallbackList<IDebugLogServiceCallback>()
private val binder = DebugLogServiceStub(this)
+ override fun onCreate() {
+ super.onCreate()
+ if (AndroidUtil.isOOrLater) forceForeground()
+ }
+
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(newBase?.getContextWithLocale(AppContextProvider.locale))
}
@@ -148,7 +155,18 @@ class DebugLogService : Service(), Logcat.Callback, Runnable {
builder.setSmallIcon(R.drawable.ic_stat_vlc)
builder.setContentIntent(pi)
val notification = builder.build()
- startForegroundCompat(3, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
+ try {
+ val type = when {
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
+ else -> 0
+ }
+ startForegroundCompat(46, notification, type)
+ } catch (e: Exception) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
+ Log.w("DebugLogService", "ForegroundServiceStartNotAllowedException caught! ${e.message}", e)
+ }
+ }
}
@Synchronized
More information about the Android
mailing list