Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -25,6 +26,7 @@ public class MusicService extends HeadlessJsTaskService {

MusicManager manager;
Handler handler;
MusicBinder binder;

@Nullable
@Override
Expand Down Expand Up @@ -56,6 +58,7 @@ public void destroy() {
manager.destroy();
manager = null;
}
binder = null;
}

private void onStartForeground() {
Expand All @@ -75,7 +78,7 @@ private void onStartForeground() {
String channel = Utils.getNotificationChannel((Context) this);

// Sets the service to foreground with an empty notification
startForeground(1, new NotificationCompat.Builder(this, channel).build());
startForeground(1, new NotificationCompat.Builder(this, channel).setPriority(NotificationCompat.PRIORITY_HIGH).build());
// Stops the service right after
stopSelf();
}
Expand All @@ -85,38 +88,34 @@ private void onStartForeground() {
@Nullable
@Override
public IBinder onBind(Intent intent) {
if(Utils.CONNECT_INTENT.equals(intent.getAction())) {
return new MusicBinder(this, manager);
}

return super.onBind(intent);
return binder;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null && Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
// Check if the app is on background, then starts a foreground service and then ends it right after
onStartForeground();

if(manager != null) {
MediaButtonReceiver.handleIntent(manager.getMetadata().getSession(), intent);
}

return START_NOT_STICKY;
}

manager = new MusicManager(this);
handler = new Handler();

super.onStartCommand(intent, flags, startId);
return START_NOT_STICKY;
}

@Override
public void onCreate() {
super.onCreate();
manager = new MusicManager(this);
handler = new Handler();
binder = new MusicBinder(this, manager);
String channel = Utils.getNotificationChannel((Context) this);
startForeground(1, new NotificationCompat.Builder(this, channel).build());
startForeground(1, new NotificationCompat.Builder(this, channel).setPriority(NotificationCompat.PRIORITY_HIGH).build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MetadataManager(MusicService service, MusicManager manager) {
this.manager = manager;

String channel = Utils.getNotificationChannel((Context) service);
this.builder = new NotificationCompat.Builder(service, channel);
this.builder = new NotificationCompat.Builder(service, channel).setPriority(NotificationCompat.PRIORITY_HIGH);
this.session = new MediaSessionCompat(service, "TrackPlayer", null, null);

session.setFlags(MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS);
Expand Down