0

I have a Service which handles the handset connection and pauses the mediaplayer if it is disconnected

here is the code

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


        }

    }

This code works perfectly well if the app is minimized and the service is running

The problem occurs if we swipe the app away from recent apps list

now refering to this https://stackoverflow.com/a/18618060/3126760

the answer says Don't ever swipe apps but this not the general behavior of users

The app would just crash and try to restart the service and again crash

My question is

How can we manage this particular lifecycle event which is neither handlled by

ondestroy on onpause

how can we manage an app which has got a service running in background and the app has been swiped out of recent apps list

I have seen apps which quietly close the app once the above event occurs

How can we just stop the app without it getting crashed.

I have even tried to manage the unregister listeners when app is swiped out using

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

and then

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();
}

does calling stopForeground(true); in ondestroy cause such an issue.

please provide an explanation for the answer you might provide, thanks.

Community
  • 1
  • 1
1234567
  • 2,226
  • 4
  • 24
  • 69
  • Can you post a StackTrace of what you refer to as _The app would just crash and try to restart the service and again crash _ – Radu Ionescu Jan 13 '16 at 10:06

1 Answers1

0

Well after a lot of search came to this conclusion

The service in onstartcommand had START_STICKY hence whenever the service was stopped it would restart unless stopService() or stopSelf() was called

my service required some getintentextras which were not found when the service was restarted

hence made a check like

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

START_NOT_STICKY wont start the service again and hence no error would occur

This is not a proper way but that is the problem with android itself.

1234567
  • 2,226
  • 4
  • 24
  • 69