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

Android 为什么MediaPlayer在用户关闭应用程序时仍继续播放?,android,android-service,android-mediaplayer,Android,Android Service,Android Mediaplayer,我已经建立了一个广播应用程序,在MediaPlayer的帮助下从流中播放现场音乐 当我创建一个服务时,问题就开始了,该服务的任务是在用户关闭应用程序时执行一段代码(其任务是删除通知)。在这个服务中,我使用startForeground()方法使它成为前台服务,从而在应用程序运行时保持它的活动状态(因为如果我不这样做,操作系统将在1分钟内杀死它) 但现在的问题是,当用户关闭应用程序时,MediaPlayer会继续播放。在服务中添加startForeground()方法之前,我的应用程序没有此行为

我已经建立了一个广播应用程序,在
MediaPlayer
的帮助下从流中播放现场音乐

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

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

我不知道这是否有帮助,但如果我在用户关闭应用程序时在onTaskRemoved()方法中添加System.exit(0),音乐停止,并且我从操作系统收到通知,说我的应用程序正在消耗电池

有人知道我的应用程序为什么会有这种奇怪的行为吗?为什么应用程序进程没有被终止

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”

您所说的“用户关闭了应用程序”是什么意思?请准确描述用户如何“关闭应用程序”@DavidWasser我的意思是用户通过从最近的应用程序列表中滑动应用程序来关闭应用程序请发布您的清单。@DavidWasser当然,我已经更新了我的帖子。确实,音乐现在停止了,但onTaskRemoved()回调方法没有执行;/我想这是我的幸运日,因为如果我为前台通知和音频播放器通知设置了相同的通知id,那么当服务停止时,通知将被删除。因此,它的工作完美<如果设置了
stopWithTask=“true”
,则不会调用code>onTaskRemoved()。在这种情况下,Android只会终止承载
服务
和其他组件(
活动
等)的操作系统进程。当您的
服务
不再作为前台
服务
运行时,Android将删除前台
服务
通知。看来你的问题解决了。