Android 将应用程序从最近的应用程序列表中刷出后,将耳机断开连接

Android 将应用程序从最近的应用程序列表中刷出后,将耳机断开连接,android,android-broadcast,headset,Android,Android Broadcast,Headset,我有一项服务,可以处理手机连接,并在mediaplayer断开连接时暂停它 这是密码 private static int headsetSwitch = 1; public static boolean headsetconnected = false; public BroadcastReceiver headsetReciever = new BroadcastReceiver() { public void onReceive(Context con

我有一项服务,可以处理手机连接,并在mediaplayer断开连接时暂停它

这是密码

   private  static int headsetSwitch = 1;
    public static boolean headsetconnected = false;

public BroadcastReceiver headsetReciever = new BroadcastReceiver() {


        public void onReceive(Context context, Intent intent) {

            if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent
                    .getAction())) {
                // Pause the playback
                mediaPlayer.pause();
            }

            if (intent.hasExtra("state")) {


                if (headsetconnected && intent.getIntExtra("state", 0) == 0) {
                    headsetconnected = false;
                    Toast.makeText(getApplicationContext(),
                            "headsetconnected = false", Toast.LENGTH_SHORT)
                            .show();
                    headsetSwitch = 0;
                }

                else if (!headsetconnected
                        && intent.getIntExtra("state", 0) == 1) {
                    headsetconnected = true;
                    Toast.makeText(getApplicationContext(),
                            "headsetconnected = true", Toast.LENGTH_SHORT)
                            .show();
                    headsetSwitch = 1;
                    // Lockscreencontrol();
                }

            }

            switch (headsetSwitch) {
            case (0):
                headsetDisconnected();
                break;
            case (1):
                break;
            }
        }

    };

    private void headsetDisconnected() {
        try {

            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                if (PlayerActivity.play != null) {
                    PlayerActivity.play
                            .setImageResource(R.drawable.ic_play_arrow_white_36dp);
                }
                if (MainActivity.play != null) {
                    MainActivity.play
                            .setImageResource(R.drawable.ic_play_arrow_white_24dp);
                }
                if (getallsongs.play != null) {
                    getallsongs.play
                            .setImageResource(R.drawable.ic_play_arrow_white_24dp);
                }

            }


        } catch (Exception e) {


        }

    }
如果应用程序最小化并且服务正在运行,则此代码可以很好地工作

如果我们将应用从“最近使用的应用”列表中刷走,则会出现问题

现在提到这个

答案是
永远不要刷应用程序
,但这不是用户的一般行为

应用程序将崩溃并尝试重新启动服务,然后再次崩溃

我的问题是

我们如何管理这个既没有人处理又没有人处理的特定生命周期事件

ondestroy
on
onpause

我们如何管理一个在后台运行服务的应用程序,并且该应用程序已从最近的应用程序列表中删除

我见过一些应用程序,一旦发生上述事件,它们就会悄悄地关闭应用程序

我们如何停止应用程序而不使其崩溃

我甚至尝试在应用程序被刷出时使用

<service
            android:name="com.musicplayer.mp3player.PlayService"
            android:enabled="true"
            android:stopWithTask="false"
            android:label="Audio-playback service" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </service>
调用
是否停止前台(true)ondestroy
中的code>会导致此类问题


请为您可能提供的答案提供一个解释,谢谢。

经过大量搜索,终于得出了这个结论

onstartcommand
中的服务具有
START\u STICKY
因此,每当服务停止时,它都会重新启动,除非调用
stopService()或stopSelf()

我的服务需要一些
getintentextras
,在服务重新启动时找不到这些文件

于是做了一个类似的检查

if (intent == null) {
            stopSelf();
            return START_NOT_STICKY;
        }
START\u NOT\u STICKY
不会再次启动服务,因此不会发生错误


这不是一个正确的方法,但这是android本身的问题。

你能发布一个StackTrace,说明你所说的应用程序会崩溃,然后尝试重新启动服务,然后再次崩溃吗_
if (intent == null) {
            stopSelf();
            return START_NOT_STICKY;
        }