0

I'm using PhoneStateListener so when the user receives a call or starts a call the speaker will be on instantly.

    PhoneStateListener phoneStateListener = new PhoneStateListener()
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber)
        {
            if (state == TelephonyManager.CALL_STATE_RINGING) 
            {


            } else if(state == TelephonyManager.CALL_STATE_IDLE)
            {

                AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

                audioManager.setSpeakerphoneOn(false);

            } else if(state == TelephonyManager.CALL_STATE_OFFHOOK)
            {

                AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                audioManager.setSpeakerphoneOn(true);
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };

    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

    if(mgr != null) 
    {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

This is the code I use. It worked fine until i've installed jelly bean, now I noticed that it sets the speaker on only when the user receives a call, but if he starts a call it does nothing

Raz
  • 160
  • 1
  • 4
  • 14
  • Weird, state should be OFFHOOK when the phone is ringing on the other side (when making an outgoing call) – Timmetje Feb 20 '13 at 15:39

1 Answers1

1

Found it.

TelephonyManager seem to only work for incoming calls.

For outgoing calls create a broadcast listener with an intent android.intent.action.NEW_OUTGOING_CALL string parametrer for the IntentFilter and don't forget to give permission in AndroidMenifest to PROCESS_OUTGOING_CALLS. This will work. Whenever there is an outgoing call the onReceive will be called in the broadcast listener.

Timmetje
  • 7,641
  • 18
  • 36
  • Hmm could you help me please im new to java and android dev. how do i make a broadcast listener? – Raz Feb 20 '13 at 16:28
  • it says 02-20 18:36:36.164: E/AndroidRuntime(19345): Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.NEW_OUTGOING_CALL from pid=19345, uid=10183 – Raz Feb 20 '13 at 16:38
  • You need permissions, in this case put this in your manifest. Also this question is answered, it isn't meant to keep you asking every point you get stuck because you don't know Java very well. Try reading the android development site. Also search SO for answers about broadcast listeners. A lot of answers already exists. For example http://stackoverflow.com/questions/13134331/cannot-detect-when-outgoing-call-is-answered-in-android. – Timmetje Feb 20 '13 at 16:46
  • Also if this answer was helpful for the above question, please mark it as answered. Don't keep asking new questions. We are not suppose to create your entire application. We help you when you have tried everything and you can't make it work. So try making your broadcast listener yourself first before asking. – Timmetje Feb 20 '13 at 16:50
  • I understand and i regret asking about the broadcast listener cause i've already found out how to make one the thing is that i did what you said from the beginning i've added "android.permission.PROCESS_OUTGOING_CALLS" to the manifest and then i got the error saying "Permission Denial..." so it's not answerd. – Raz Feb 20 '13 at 16:55
  • You asked why TelephonyManager doesn't see outgoing calls. The solution is Broadcast listener. If you can't get that to work it's a different problem. What permission denial do you get now? If you add PROCESS_OUTGOING_CALLS you wont get that denial anymore. Don't forget to put it outside the application tag. Read some documentation. http://stackoverflow.com/questions/6162081/permission-denial-requires-android-permission-write-external-storage http://stackoverflow.com/questions/13134331/cannot-detect-when-outgoing-call-is-answered-in-android and many many more.. – Timmetje Feb 20 '13 at 17:03