代码之家  ›  专栏  ›  技术社区  ›  Konstantinos Nikoloutsos

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

  •  1
  • Konstantinos Nikoloutsos  · 技术社区  · 8 年前

    我已经建立了一个广播应用程序,可以在流媒体的帮助下播放现场音乐。 MediaPlayer .

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

    但现在的问题是,当用户关闭应用程序时, 媒体播放器 继续玩。我的应用程序在添加 开始前景() 服务中的方法。

    我不知道这是否有帮助,但如果我在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();
        }
    
    }
    

    清单.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>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   David Wasser    8 年前

    您的清单中有“stopwithtask=”false“”。这会告诉Android,当用户从最近任务列表中刷出应用程序时,不要停止您的服务。设置为“真”