2

I want to play background music when application was killed from recent task menu in android studio.

I have tried using background services...lower devices application worked fine but in android pie after 5 sec song will stopped.

public class Myservice extends Service  {
MediaPlayer mediaPlayer;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {

    Toast.makeText(this, "service started successfully", Toast.LENGTH_SHORT).show();
    mediaPlayer = MediaPlayer.create(this, animal);
    mediaPlayer.setLooping(false);

    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {

    Toast.makeText(this, "Mediaplayer started", Toast.LENGTH_SHORT).show();
    mediaPlayer.start();

    super.onStart(intent, startId);
}

@Override
public void onDestroy() {

    mediaPlayer.stop();

    }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Herry patel
  • 39
  • 1
  • 3
  • 1
    use startForeground instead of the old startService and attach a permanent notification to it according to the documentation here:https://developer.android.com/guide/components/services and https://developer.android.com/guide/components/services#Foreground – Nikos Hidalgo Sep 13 '19 at 10:53

3 Answers3

3

Try to read about Background Execution Limits and Background Service Limitations.

From Android 8 (API level 26) there are background restrictions on what your apps can do while running in the background:

Whenever an app runs in the background, it consumes some of the device's limited resources, like RAM. This can result in an impaired user experience, especially if the user is using a resource-intensive app, such as playing a game or watching video. To improve the user experience, Android 8.0 (API level 26) imposes limitations on what apps can do while running in the background.


In your case, your pre android 8 devices can run with background service and it works.

But for any device with android 8 and above you can`t do it because the system will "kill" your background task - this is why after 5 seconds your song will be stopped.

You can solve this by using a Foreground service:

A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.

You can find many examples of how to use a Foreground Service like Foreground Service Android Example or this thread

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
2

I would like to add some extra information @Tamir Abutbul Answer, as he told that You can solve this by using a Foreground service.

Apps that target Android 9 (API level 28) or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app.

If an app that targets API level 28 or higher attempts to create a foreground service without requesting FOREGROUND_SERVICE, the system throws a SecurityException.

please refer this link. https://developer.android.com/guide/components/services#Foreground

gautam
  • 522
  • 3
  • 14
0

You need to give administrative permission to run application in background(enable app in background, u can find in battery optimizations) and as well as you have to give autostart permission(when device reboots to start the service)

Sai
  • 278
  • 3
  • 7