Question

I would like to turn on the speaker and set it to maximum volume. In my PhoneStateListener I'm intercepting the incoming call, and it works fine for any incoming/outgoing call.

The thing is that I want to turn on this feature only for two specific incoming numbers.

This is my code:

    case TelephonyManager.CALL_STATE_OFFHOOK:
        if (incomingNumber.equals( strRegisterNumber1) || incomingNumber.equals( strRegisterNumber2))
        {
            AudioManager audioManager = (AudioManager) contextMember.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setSpeakerphoneOn(true);
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);

        }
        break;

Without the if statement it works fine but int his case the speaker stays off.

Please advise what I'm doing wrong or how to achieve my goal?

Thank you for your help.

Was it helpful?

Solution

Instaed of

incomingNumber.equals( strRegisterNumber1) || incomingNumber.equals( strRegisterNumber2) 

use

incomingNumber.contains( strRegisterNumber1) || incomingNumber.contains( strRegisterNumber2)

Because the incoming number might have + with country code or 00 with country code. So you just have to check if the incoming call's number contains the required number.

Hope it helps...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top