Skip to content

Commit bcc5548

Browse files
committed
Actually Fixed: Context.startForegroundService() did not then call Service.startForeground()
Signed-off-by: AR-Calder <[email protected]>
1 parent e8cd55f commit bcc5548

File tree

1 file changed

+67
-49
lines changed

1 file changed

+67
-49
lines changed

app/src/main/java/uk/arcalder/Kanta/MusicPlayerService.java

+67-49
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,13 @@ public void onPlay() {
157157
//Should be started but sometimes not :s
158158
if(!isServiceStarted){
159159
Log.d(TAG, "onPlay: Starting Service");
160-
startService(new Intent(getApplicationContext(), MusicPlayerService.class));
160+
//startForegroundService(new Intent(getApplicationContext(), MusicPlayerService.class));
161+
currentNotification = buildNotification();
162+
Log.d(TAG, "------------------------------------------onPLAY create notification-------------------------------------------");
163+
startForeground(NOTIFICATION_ID, currentNotification);
161164
}
162165

163-
if(mPlaybackState.getState() == PlaybackStateCompat.STATE_PAUSED) { // If we are resuming something not trying to play an un-started song
166+
if(mMediaController.getPlaybackState().getState() == PlaybackStateCompat.STATE_PAUSED) { // If we are resuming something not trying to play an un-started song
164167
Log.d(TAG, "onPlay: Starting Player");
165168

166169
//Doesn't update itself
@@ -170,17 +173,21 @@ public void onPlay() {
170173
initNoisyReceiver();
171174

172175
currentNotification = buildNotification();
173-
mNotificationManager.notify(NOTIFICATION_ID, currentNotification);
176+
Log.d(TAG, "------------------------------------------onPLAY Notify-------------------------------------------");
177+
startForeground(NOTIFICATION_ID, currentNotification);
174178
}
175179

176180

177181

178-
super.onPlay();
182+
183+
//super.onPlay();
179184
// TODO NOTE: onPlay is really onResume/onNowPlaying, don't try to play songs here
185+
Log.d(TAG, "------------------------------------------onPLAY Notified-------------------------------------------");
180186
}
181187

182188
@Override
183189
public void onPause() {
190+
Log.d(TAG, "------------------------------------------onPause-------------------------------------------");
184191
super.onPause();
185192

186193
if( mMediaPlayer.isPlaying() ) {
@@ -200,6 +207,7 @@ public void onPause() {
200207

201208
@Override
202209
public void onPlayFromMediaId(String data, Bundle extras) {
210+
Log.d(TAG, "------------------------------------------onPlayFromMediaId-------------------------------------------");
203211
Log.d(TAG, "onPlayFromMediaId");
204212
super.onPlayFromMediaId(data, extras);
205213

@@ -215,7 +223,7 @@ public void onPlayFromMediaId(String data, Bundle extras) {
215223
//Should be started but sometimes not :s
216224
if(!isServiceStarted){
217225
Log.d(TAG, "onPlayFromMediaId: Starting Service");
218-
startService(new Intent(getApplicationContext(), MusicPlayerService.class));
226+
startForegroundService(new Intent(getApplicationContext(), MusicPlayerService.class));
219227
}
220228

221229
Log.d(TAG, "onPlayFromMediaId: data is " + data);
@@ -235,16 +243,20 @@ public void onPlayFromMediaId(String data, Bundle extras) {
235243
// //afd.close();
236244
// initMediaSessionMetadata();
237245
// Notify fragments
238-
mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
246+
//mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
239247
}
240248

241249
@Override
242250
public void onCommand(String command, Bundle extras, ResultReceiver cb) {
251+
Log.d(TAG, "------------------------------------------onCommand-------------------------------------------");
243252
super.onCommand(command, extras, cb);
244253
// if( COMMAND_EXAMPLE.equalsIgnoreCase(command) ) {
245254
// //Custom command here
246255
// }
256+
247257
mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
258+
currentNotification = buildNotification();
259+
startForeground(NOTIFICATION_ID, currentNotification);
248260
}
249261

250262
@Override
@@ -254,6 +266,7 @@ public void onSeekTo(long pos) {
254266

255267
@Override
256268
public void onStop() {
269+
Log.d(TAG, "------------------------------------------on STOP-------------------------------------------");
257270
Log.d(TAG, "onStop called");
258271
releaseAudioFocus();
259272
setMediaPlaybackState(PlaybackStateCompat.STATE_STOPPED);
@@ -266,13 +279,14 @@ public void onStop() {
266279
stopForeground(true);
267280

268281
// Notify fragments
269-
mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
282+
//mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
270283
super.onStop();
271284

272285
}
273286

274287
@Override
275288
public void onSkipToNext() {
289+
Log.d(TAG, "------------------------------------------onSkipToNext-------------------------------------------");
276290
Song nextSong = mMusicLibrary.getNextSong(); // TODO GET NEXT SONG
277291
if (null != nextSong) {
278292
Log.d(TAG, "onSkipToNext: nextSong is '" + nextSong.getTitle() + "'");
@@ -285,12 +299,13 @@ public void onSkipToNext() {
285299
}
286300

287301
// Notify fragments
288-
mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
302+
//mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
289303
super.onSkipToNext();
290304
}
291305

292306
@Override
293307
public void onSkipToPrevious() {
308+
Log.d(TAG, "------------------------------------------onSkipToPrevious-------------------------------------------");
294309
Song prevSong = mMusicLibrary.getPreviousSong(); // TODO GET PREV SONG
295310
if (null != prevSong) {
296311
Log.d(TAG, "onSkipToPrevious: prevSong is '" + prevSong.getTitle() + "'");
@@ -302,7 +317,7 @@ public void onSkipToPrevious() {
302317
}
303318

304319
// Notify fragments
305-
mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
320+
//mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
306321
super.onSkipToPrevious();
307322
}
308323

@@ -311,6 +326,7 @@ public void onSkipToPrevious() {
311326

312327
@Override
313328
public void onCreate() {
329+
Log.d(TAG, "------------------------------------------onCreate-------------------------------------------");
314330
super.onCreate();
315331

316332
// --------------------------External Communications----------------------
@@ -370,25 +386,27 @@ public void onCreate() {
370386

371387
// Shite fix for shite issue
372388
//https://stackoverflow.com/questions/44425584/context-startforegroundservice-did-not-then-call-service-startforeground
373-
if (Build.VERSION.SDK_INT >= 26) {
374-
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
375-
CHANNEL_ID,
376-
NotificationManager.IMPORTANCE_MIN);
377-
378-
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
379-
380-
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
381-
.setContentTitle("")
382-
.setContentText("").build();
383-
384-
startForeground(1, notification);
385-
}
389+
// if (Build.VERSION.SDK_INT >= 26) {
390+
// NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
391+
// CHANNEL_ID,
392+
// NotificationManager.IMPORTANCE_MIN);
393+
//
394+
// ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
395+
//
396+
// Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
397+
// .setContentTitle("")
398+
// .setContentText("").build();
399+
//
400+
// startForeground(1, notification);
401+
// }
386402

387403
Log.d(TAG, "onCreate MusicService creating MediaSession, MediaPlayer, and MediaNotificationManager");
388404
}
389405

390406
@Override
391407
public void onDestroy() {
408+
Log.d(TAG, "------------------------------------------onDestroy-------------------------------------------");
409+
392410
Log.d(TAG, "onDestroy");
393411
releaseAudioFocus();
394412
removeNoisyReceiver();
@@ -397,16 +415,16 @@ public void onDestroy() {
397415
mMediaPlayer.reset();
398416
Log.d(TAG, "onDestroy: mediaplayer.release()");
399417
mMediaPlayer.release();
400-
mMediaPlayer = null;
418+
//mMediaPlayer = null;
401419
}
402420
if (mMediaSession != null) {
403421
Log.d(TAG, "onDestroy: mediasession.release()");
404422
mMediaSession.release();
405-
mMediaSession = null;
423+
//mMediaSession = null;
406424
}
407425

408-
Log.d(TAG, "onDestroy: stop Foreground");
409-
stopForeground(true);
426+
// Log.d(TAG, "onDestroy: stop Foreground");
427+
// stopForeground(true);
410428

411429

412430
//Log.d(TAG, "onDestroy: cancelInitSongs");
@@ -420,6 +438,7 @@ public void onDestroy() {
420438

421439
@Override
422440
public void onTaskRemoved(Intent rootIntent) {
441+
Log.d(TAG, "------------------------------------------on TASK REMVOED-------------------------------------------");
423442
Log.d(TAG, "onTaskRemoved");
424443

425444
// TODO if for some reason the app is closed before the library is able to load
@@ -437,6 +456,7 @@ public void onTaskRemoved(Intent rootIntent) {
437456
}
438457

439458
private void initMediaController() {
459+
Log.d(TAG, "------------------------------------------initMediaController-------------------------------------------");
440460
try {
441461
mMediaController = new MediaControllerCompat(getApplicationContext(), mMediaSession.getSessionToken());
442462
} catch (Exception e) {
@@ -449,13 +469,15 @@ private void initMediaController() {
449469
private static boolean isNoisyReceiverActive = false;
450470

451471
private void initNoisyReceiver() {
472+
Log.d(TAG, "------------------------------------------initNoisyReceiver-------------------------------------------");
452473
//Handles headphones coming unplugged. cannot be done through a manifest receiver
453474
IntentFilter filter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
454475
registerReceiver(mNoisyReceiver, filter);
455476
isNoisyReceiverActive = true;
456477
}
457478

458479
private void removeNoisyReceiver(){
480+
Log.d(TAG, "------------------------------------------removeNoisyReceiver-------------------------------------------");
459481
if (isNoisyReceiverActive){
460482
Log.d(TAG, "removeNoisyReceiver: removing");
461483
unregisterReceiver(mNoisyReceiver);
@@ -467,6 +489,7 @@ private void removeNoisyReceiver(){
467489
}
468490

469491
private void initMediaPlayer() {
492+
Log.d(TAG, "------------------------------------------initMediaPlayer-------------------------------------------");
470493
mAudioManager = (mAudioManager == null) ? (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE) : mAudioManager;
471494
if ( mMediaPlayer == null) {
472495
mMediaPlayer = new MediaPlayer();
@@ -483,6 +506,7 @@ private void initMediaPlayer() {
483506
}
484507

485508
private void initMediaSession() {
509+
Log.d(TAG, "------------------------------------------initMediaSession-------------------------------------------");
486510
ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
487511
// Create a MediaSession
488512
mMediaSession = new MediaSessionCompat(getApplicationContext(), TAG);
@@ -525,23 +549,8 @@ private void setMediaPlaybackState(int state) {
525549
mMediaSession.setPlaybackState(playbackStateBuilder.build());
526550
}
527551

528-
private void initMediaSessionMetadata() {
529-
MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder();
530-
//Notification icon in card
531-
metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
532-
metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
533-
534-
//lock screen icon for pre lollipop
535-
metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
536-
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, "Display Title");
537-
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, "Display Subtitle");
538-
metadataBuilder.putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, 1);
539-
metadataBuilder.putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, 1);
540-
541-
mMediaSession.setMetadata(metadataBuilder.build());
542-
}
543-
544552
private boolean getAudioFocus() {
553+
Log.d(TAG, "------------------------------------------getAudioFocus-------------------------------------------");
545554
Log.d(TAG, "tryToGetAudioFocus");
546555
int result = mAudioManager.requestAudioFocus(mAudioFocusRequest);
547556
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
@@ -556,6 +565,7 @@ private boolean getAudioFocus() {
556565
}
557566

558567
private void releaseAudioFocus() {
568+
Log.d(TAG, "------------------------------------------releaseAudioFocus-------------------------------------------");
559569
Log.d(TAG, "giveUpAudioFocus");
560570
if (mAudioManager.abandonAudioFocusRequest(mAudioFocusRequest)
561571
== AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
@@ -569,6 +579,7 @@ private void releaseAudioFocus() {
569579

570580
@Override
571581
public void onAudioFocusChange(int focusChange) {
582+
Log.d(TAG, "------------------------------------------onAudioFocusChange-------------------------------------------");
572583
Log.d(TAG, "onAudioFocusChange. focusChange: "+ focusChange);
573584
switch (focusChange) {
574585
case AudioManager.AUDIOFOCUS_GAIN:
@@ -634,6 +645,7 @@ public void onCompletion(MediaPlayer mediaPlayer) {
634645

635646
@Override
636647
public int onStartCommand(Intent intent, int flags, int startId) {
648+
Log.d(TAG, "------------------------------------------onStartCommand-------------------------------------------");
637649
Log.d(TAG, "onStartCommand");
638650
isServiceStarted = true;
639651
// TODO USE APP COMPAT EVERYTHING
@@ -645,6 +657,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
645657

646658
@Override
647659
public void onPrepared(MediaPlayer mediaPlayer) {
660+
Log.d(TAG, "------------------------------------------onPrepared-------------------------------------------");
648661
Log.d(TAG, "onPrepared");
649662
mMediaSession.setActive(true);
650663
mediaPlayer.start();
@@ -656,7 +669,7 @@ public void onPrepared(MediaPlayer mediaPlayer) {
656669
ContextCompat.startForegroundService(this, intent);
657670

658671
currentNotification = buildNotification();
659-
this.startForeground(NOTIFICATION_ID, currentNotification);
672+
startForeground(NOTIFICATION_ID, currentNotification);
660673
}
661674

662675
// ------------------------------Notifications--------------------------------
@@ -665,6 +678,8 @@ public void onPrepared(MediaPlayer mediaPlayer) {
665678

666679
private Notification buildNotification(){
667680

681+
Log.d(TAG, "------------------------------------------NOTIFICATION-------------------------------------------");
682+
668683
Log.d(TAG, "Creating notification");
669684

670685
// Get current song
@@ -706,7 +721,8 @@ private Notification buildNotification(){
706721
.setContentIntent(createContentIntent(current_song))
707722
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
708723
.setLargeIcon(albumArt)
709-
.setSmallIcon((mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play)
724+
//.setSmallIcon((mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play)
725+
.setSmallIcon(R.drawable.icons8musicalnotes24)
710726
// Title - Usually Song name.
711727
.setContentTitle(current_song.getTitle())
712728
// Subtitle - Usually Artist name.
@@ -751,19 +767,21 @@ private Notification buildNotification(){
751767
.setMediaSession(mMediaSession.getSessionToken())
752768
.setShowActionsInCompactView(0, 1, 2));
753769

754-
if (null == mPlaybackState){
755-
Log.d(TAG, "null == playbackState OR !notificationStarted");
756-
// "Die die die"
757-
this.stopForeground(true);
758-
}
770+
// if (null == mPlaybackState){
771+
// Log.d(TAG, "null == playbackState OR !notificationStarted");
772+
// // "Die die die"
773+
// this.stopForeground(true);
774+
// }
759775

760776
Log.d(TAG, "createNotificationChannel: build");
761777

778+
Log.d(TAG, "-----------------------------------NOTIFICATION BUILT-------------------------------------------");
762779
return notificationBuilder.build();
763780
}
764781

765782
// Set the notification's tap action
766783
private PendingIntent createContentIntent(Song song){
784+
Log.d(TAG, "------------------------------------------createContentIntent-------------------------------------------");
767785
Intent openUI = new Intent(this, MainActivity.class);
768786
// IF encountering problems return to the stack overflow post:
769787
// https://stackoverflow.com/questions/29321261/what-are-the-differences-between-flag-activity-reset-task-if-needed-and-flag-act

0 commit comments

Comments
 (0)