将应用从最近的应用列表中删除后处理耳机已断开

时间:2016-01-13 09:25:53

标签: android android-broadcast headset

我有一个处理手机连接的服务,并在媒体播放器断开连接时暂停它

这是代码

   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) {


        }

    }

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

如果我们将应用从最近的应用列表中移除

,就会出现问题

现在引用此https://stackoverflow.com/a/18618060/3126760

答案是Don't ever swipe apps,但这不是用户的一般行为

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

我的问题是

我们如何管理这个既未被

处理的特定生命周期事件 ondestroy

上的

onpause

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

我看过上述事件发生后悄然关闭应用的应用

如何在不崩溃的情况下停止应用程序。

我甚至试图在使用

删除应用时管理unregister listeners
<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>

然后

public void onTaskRemoved(Intent rootIntent){

    // unregister listeners
    // do any other cleanup if required
    try {




        unregisterReceiver(headsetReciever);
    } catch (Exception e) {
        // TODO: handle exception
    }


    // stop service stopSelf();
}

stopForeground(true);中调用ondestroy会导致此类问题。

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

1 个答案:

答案 0 :(得分:0)

经过大量搜索后得出这个结论

onstartcommand中的服务有START_STICKY因此,只要服务停止,它就会重新启动,除非调用stopService() or stopSelf()

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

因此做了一个像

的检查
if (intent == null) {
            stopSelf();
            return START_NOT_STICKY;
        }

START_NOT_STICKY不会再次启动服务,因此不会发生错误

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

相关问题