0

Possible Duplicate:
Stopping & Starting music on incoming calls

I have a simple application that plays some audio using the MediaPlayer class. While debugging my application I found that the audio still plays during incoming and outgoing calls.

I found that the solution to my problem is using a phoneStateListener. I don't quite understand how to do this though. I saw some examples that used a BroadcastReceiver along with the PhoneStateListener and I couldn't understand why we needed a BroadCastReceiver.

I would appreciate it if someone could provide a sample implementation of the PhoneStateListener. If I can get the phone state I could pause or resume the audio because my MediaPlayer object is global for the entire application.

Community
  • 1
  • 1
RagHaven
  • 4,156
  • 21
  • 72
  • 113
  • I saw that post. Where exactly do I add that code ? I dont understand where to add it and how the PhoneStateReciever will get called. – RagHaven Jun 20 '12 at 14:08

1 Answers1

1

This is what I use... The onCallStateChanged() function gets called in all PhoneStateListeners anytime the call state has changed, so that's what you need to handle.

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

//Makes sure the audio is paused for incoming/outgoing phone calls
public class ListenToPhoneState extends PhoneStateListener {

    private boolean pausedForPhoneCall = false;
    private UIManager uiManager;

    ListenToPhoneState(UIManager manager){
        uiManager = manager;
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                resumeInAndroid();
                return;
            case TelephonyManager.CALL_STATE_OFFHOOK: 
                pauseInAndroid();               
                return;
            case TelephonyManager.CALL_STATE_RINGING: 
                pauseInAndroid();               
                return;
        }
    }

    private void resumeInAndroid(){
        if(pausedForPhoneCall == true) {
            pausedForPhoneCall=false;
            uiManager.waitForPhoneCall(false);
        }
    }

    private void pauseInAndroid(){
        if(pausedForPhoneCall == false){
            pausedForPhoneCall=true;
            uiManager.waitForPhoneCall(true);
        }
    }

    String stateName(int state) {
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE: return "Idle";
            case TelephonyManager.CALL_STATE_OFFHOOK: return "Off hook";
            case TelephonyManager.CALL_STATE_RINGING: return "Ringing";
        }
        return Integer.toString(state);
    }
}

If you want to add this to your current Activity, do this..

ListenToPhoneState listener = new ListenToPhoneState(userInterface);
        TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if(tManager != null)
            tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

Wouldn't you rather just pause your current activity? If you pass your class an instance of the current activity, you could call finish() and create a new activity right in the switch statement if you really want to.

Joel
  • 4,732
  • 9
  • 39
  • 54
  • So does android automatically call the onCallStateChanged Function whenever there is a phone call ? – RagHaven Jun 20 '12 at 15:35
  • yes, when you extend the PhoneStateListener class, you are registering that class with the OS, and if you Override the onCallStateChanged function, that function will get called by the OS anytime the phone call state is changed. – Joel Jun 20 '12 at 15:55
  • Oh, thanks! Is there anyway I can start an activity using an intent within this class ? So in case OFF_HOOK i would like to start a different activity. – RagHaven Jun 20 '12 at 16:06
  • Hi Joel, is there any way to get the events of Viber Calling or Skype etc.. – ravi Apr 24 '15 at 07:14