diff options
author | Max Kellermann <max@musicpd.org> | 2021-05-31 20:37:10 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2021-05-31 20:45:31 +0200 |
commit | ab487b9a99f9438bd147e0c3a4d7467287a4c0d8 (patch) | |
tree | f152564ba9a442d1f1ce90466340ffa81ea9a5bc | |
parent | ac59ec34f9d467b423b1f5f68bdfb2e7f2f944c3 (diff) |
Android: use startForegroundService() in Android 8+
Fixes the error:
IllegalStateException: Not allowed to start service Intent { cmp=org.musicpd/.Main (has extras) }: app is in background
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | android/src/Main.java | 11 |
2 files changed, 12 insertions, 1 deletions
@@ -4,6 +4,8 @@ ver 0.22.9 (not yet released) * decoder - ffmpeg: support the tags "sort_album", "album-sort", "artist-sort" - ffmpeg: fix build failure with FFmpeg 3.4 +* Android + - fix auto-start on boot in Android 8 or later * Windows - fix build failure with SQLite diff --git a/android/src/Main.java b/android/src/Main.java index 2c307811a..15c7ba419 100644 --- a/android/src/Main.java +++ b/android/src/Main.java @@ -414,6 +414,15 @@ public class Main extends Service implements Runnable { * start Main service without any callback */ public static void start(Context context, boolean wakelock) { - context.startService(new Intent(context, Main.class).putExtra("wakelock", wakelock)); + Intent intent = new Intent(context, Main.class) + .putExtra("wakelock", wakelock); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + /* in Android 8+, we need to use this method + or else we'll get "IllegalStateException: + app is in background" */ + context.startForegroundService(intent); + else + context.startService(intent); } } |