2
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(description.getTitle())
                .setAutoCancel(false);
    NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notificationCompat=builder.build();
    notificationManager.notify(MY_ID,notificationCompat);
    startForeground(MY_ID,notificationCompat);

Used above code to run service in background. But when activity closed, service call onDestroy

Bincy Baby
  • 3,941
  • 5
  • 36
  • 62

1 Answers1

0

You can extend your service from IntentService, so it's working with separate thread and setOngoing true to your notification

builder.setOngoing(true)

After this user can't remove notification manually, and it supports the service in working condition. And when activity closed your service will continue to work.

Levon Vardanyan
  • 400
  • 4
  • 16
  • Its an media player service, playing in background – Bincy Baby Jan 25 '17 at 13:43
  • If i understand correctly, you mean that your service can't be extended from IntentService, because media player service extends from Service (https://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices) . So try to set setOngoing(true) to your notification, without extending from IntentService. – Levon Vardanyan Jan 25 '17 at 14:29