Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneStateListener is not working in android lollipop

I've developed a radio app where i need to pause radio when incoming/outgoing phone calls comes. I've used PhoneStateListener which is working fine in all android version except in lollipop. Here is the code that i have used to handle phone calls. I've tried both setStreamVolume and setStreamMute methods but both seems not working in lollipop. Please some body help me on this.

Thanks in advance.

                PhoneStateListener phoneStateListener = new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state,
                        String incomingNumber) {
                    switch (state) {

                    case TelephonyManager.CALL_STATE_RINGING: // Incoming
                                                                // call:
                                                                // Pause
                                                                // music
                        if (Globals.isPlayerRunning) {
                            curVolume = audioManager
                                    .getStreamVolume(AudioManager.STREAM_MUSIC);
                            audioManager.setStreamVolume(
                                    AudioManager.STREAM_MUSIC, 0, 0);
                            audioManager.setStreamMute(
                                    AudioManager.STREAM_MUSIC, true);
                            isServiceMuteByCall = true;
                        }
                        break;

                    case TelephonyManager.CALL_STATE_IDLE: // Not in
                                                            // call:
                                                            // Play
                                                            // music
                        if (isServiceMuteByCall) {
                            isServiceMuteByCall = false;
                            audioManager.setStreamMute(
                                    AudioManager.STREAM_MUSIC, false);
                            audioManager.setStreamVolume(
                                    AudioManager.STREAM_MUSIC, curVolume,
                                    AudioManager.FLAG_PLAY_SOUND);
                        }
                        break;

                    case TelephonyManager.CALL_STATE_OFFHOOK:// A call
                                                                // is
                                                                // dialing,
                                                                // active
                                                                // or
                                                                // on
                                                                // hold
                        if (Globals.isPlayerRunning) {
                            curVolume = audioManager
                                    .getStreamVolume(AudioManager.STREAM_MUSIC);
                            audioManager.setStreamMute(
                                    AudioManager.STREAM_MUSIC, true);
                            audioManager.setStreamVolume(
                                    AudioManager.STREAM_MUSIC, 0, 0);
                            isServiceMuteByCall = true;
                        }
                        break;
                    }
                    super.onCallStateChanged(state, incomingNumber);

                }
            };

            if (mgr != null) {
                mgr.listen(phoneStateListener,
                        PhoneStateListener.LISTEN_CALL_STATE);
            }
like image 278
Ramesh Avatar asked Oct 19 '22 18:10

Ramesh


1 Answers

Here is the solution to above issue. This can be solved using BroadcastReceiver.

Create a class which extends BroadcastReceiver and inside onreceive method check for phone states like below.

public class TelephonyManagerReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

         if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction())) {

             String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

             if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
                 //stop the player or mute the audio here
             } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
                 //start the player or unmute the audio here
             } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
                 //stop the player or mute the audio here
             }
        }   
    }   
}

after this add above broadcast receiver in manifest file.

<receiver android:name="com.radioapp.util.TelephonyManagerReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
 </receiver>

and ensure that you request this permission in the manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>  
like image 118
Ramesh Avatar answered Nov 03 '22 02:11

Ramesh