Skip to content

Commit fbddb87

Browse files
committed
RemoteControlService: Fix ForegroundServiceStartNotAllowedException
1 parent 2296d0a commit fbddb87

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/misc/RemoteControlService.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import android.content.Intent;
66
import android.os.Build.VERSION;
77
import android.os.IBinder;
8+
import android.support.v4.media.session.MediaSessionCompat;
9+
810
import androidx.annotation.Nullable;
11+
12+
import com.liskovsoft.sharedutils.helpers.AppInfoHelpers;
913
import com.liskovsoft.sharedutils.helpers.Helpers;
1014
import com.liskovsoft.sharedutils.mylogger.Log;
1115
import com.liskovsoft.smartyoutubetv2.common.R;
@@ -29,17 +33,16 @@ public IBinder onBind(Intent intent) {
2933
public void onCreate() {
3034
super.onCreate();
3135

32-
//// https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten
33-
//if (VERSION.SDK_INT >= 26) {
34-
// // NOTE: it's impossible to hide notification on Android 9 and above
35-
// // https://stackoverflow.com/questions/10962418/how-to-startforeground-without-showing-notification
36-
// startForeground(NOTIFICATION_ID, createNotification());
37-
//}
38-
3936
// https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten
4037
// NOTE: it's impossible to hide notification on Android 9 and above
4138
// https://stackoverflow.com/questions/10962418/how-to-startforeground-without-showing-notification
4239
try {
40+
if (AppInfoHelpers.getRealSdkVersion(this) >= 34) { // Fix ForegroundServiceStartNotAllowedException
41+
// Tie service to active media session
42+
MediaSessionCompat mediaSession = new MediaSessionCompat(this, TAG);
43+
mediaSession.setActive(true);
44+
}
45+
4346
startForeground(NOTIFICATION_ID, createNotification());
4447
} catch (NullPointerException e) {
4548
// NullPointerException: Attempt to read from field 'int com.android.server.am.UidRecord.curProcState' on a null object reference

0 commit comments

Comments
 (0)