Turn ON speaker in incoming call Android

主宰稳场 提交于 2020-01-04 03:01:24

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/16188831/turn-on-speaker-in-incoming-call-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!