简体   繁体   中英

Turn ON speaker in incoming call Android

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM