为什么 MediaPlayer 在用户关闭应用程序时继续播放?

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

我构建了一个广播应用程序,它在 MediaPlayer 的帮助下播放流中的现场音乐。

当我创建一个服务时,问题就开始了,该服务的工作是在用户关闭应用程序时执行一段代码(它的工作是删除通知)。在此服务中,我使用 startForeground() 方法使其成为 前台服务 并因此只要应用程序运行就使其保持活动状态(因为如果我不这样做,OS 会在 1 分钟内杀死它)。

但现在的问题是,当用户关闭应用程序时,MediaPlayer 继续播放。在服务中添加 startForeground() 方法之前,我的应用程序没有此行为。

我不知道这是否有帮助,但如果我在 onTaskRemoved() 方法中添加一个 System.exit(0) 当应用程序被用户关闭时音乐停止并且我收到来自 OS 表示我的应用正在耗电。

有谁知道为什么我的应用程序有这种奇怪的行为?为什么应用进程没有被杀死??

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>

您的清单中有 'stopWithTask="false"'。这告诉 Android 当用户从最近的任务列表中滑动应用程序时不要停止您的服务。将其设置为 "true"