shakil.k
shakil.k

Reputation: 1723

Foreground Service get killed after Android Screen Unlock

I am Playing Music in Foreground Service and it is bounded with the Activity and when the Activity is running Service will not kill if screen get off and on again but when the Activity is not visible means Music is only Running in Foreground Service and app is close while music is playing in the background and when I turn the screen off the music is playing fine but when I unlock the screen it killed the Service

Upvotes: 1

Views: 1303

Answers (1)

matteolel
matteolel

Reputation: 519

Looking at this comment at the page provided in the @corsair992 comment, I found this solution that solve the problem for me!

AlarmManager almgr = (AlarmManager)MyContext.getSystemService(Context.ALARM_SERVICE);
Intent timerIntent = new Intent(MyUniqueLabel);
timerIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
PendingIntent pendingOffLoadIntent = PendingIntent.getBroadcast(MyContext, 1, timerIntent, 0);

you MUST do these things for it to work.

1.) Call addFlags and the intent and pass it in FLAG_RECEIVER_FORGROUND

2.) Use a non-zero request code in PendingIntent.getBroadcast

If you leave any of those steps out it will not work.

Upvotes: 1

Related Questions