Search code examples
androidandroid-serviceandroid-mediaplayer

Why MediaPlayer keeps playing when app is closed by the user?


I've built a radio app which plays live music from a stream with the help of MediaPlayer.

The problem started when I created a service which job is to execute a block of code when app is closed by the user(Its job is to delete the notification). In this service I am using startForeground() method in order to make it as a foreground service and therefore keep it alive as long as the app runs (because if I don't, the OS will kill it in 1 minute).

But the problem now is that when the user closes the app the MediaPlayer keeps playing. My app didn't have this behavior before adding the startForeground() method in the service.

I don't know if this would be helpful but if I add a System.exit(0) in the onTaskRemoved() method when app is closed by the user music stops and I get a notification from OS which says that my app is consuming battery.

Does anyone know why my application has this peculiar behavior? Why the application process isn't killed??

public class OnClearFromRecentService extends Service {
    private static final String TAG = "onClearFromRecentServic";
    private NotificationManagerCompat mNotificationManagerCompat;

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Service Started");
        startForeground(2001, new NotificationCompat.Builder(getApplicationContext(), CHANNELID)
    .setContentTitle("title").setContentText("contentText").build());
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Service Destroyed");
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        //Put code here which will be executed when app is closed from user.
        Log.d(TAG, "onTaskRemoved was executed ");
        mNotificationManagerCompat.cancelAll();
        stopSelf();
    }

}

Manifest.xml :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nikolouts.kwnstantinos.plutoradio">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"></activity>
        <activity
            android:name=".AboutActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".SplashScreenActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />

        <receiver android:name=".MainActivity$NotificationReceiver" />

        <activity android:name=".ChatActivity"/>
        <service android:name=".OnClearFromRecentService" android:stopWithTask="false" />

    </application>

</manifest>

Solution

  • You have in your manifest 'stopWithTask="false"'. This tells Android not to stop your Service when the user swipes the app from the list of recent tasks. Set this to "true"