55import android .content .Intent ;
66import android .os .Build .VERSION ;
77import android .os .IBinder ;
8+ import android .support .v4 .media .session .MediaSessionCompat ;
9+
810import androidx .annotation .Nullable ;
11+
12+ import com .liskovsoft .sharedutils .helpers .AppInfoHelpers ;
913import com .liskovsoft .sharedutils .helpers .Helpers ;
1014import com .liskovsoft .sharedutils .mylogger .Log ;
1115import 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