setRingerMode to normall during incoming call doesn't work on Samsung devices

左心房为你撑大大i 提交于 2019-12-11 03:55:50

问题


I've read here. Didn't find the answer.

public class CallReceiver extends BroadcastReceiver {

private TelephonyManager mTelephony;
private IncomingPhoneStateListener mIncomingPhoneListener;
private String number;
private Context mContext;

@Override
public void onReceive(Context context, Intent intent) {
    try {
        mContext = context;
        number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        mIncomingPhoneListener = new IncomingPhoneStateListener();
        mTelephony.listen(mIncomingPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    } catch (Exception e) {
        Log.e("CallReceiver", "onReceive error:" + e.getMessage());
    }
}

public class IncomingPhoneStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:

                // ******* This code should unmute and change volume to MAX
                am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                am.setStreamVolume(AudioManager.STREAM_RING, am.getStreamMaxVolume(AudioManager.STREAM_RING), AudioManager.FLAG_PLAY_SOUND);
                // ******* end
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                break;
            default:
                break;
        }
        super.onCallStateChanged(state, incomingNumber);
        mTelephony.listen(mIncomingPhoneListener, PhoneStateListener.LISTEN_NONE); // unregister
    }
}

}

When the device is on silent mode or vibration mode, above code unmute and change the volume to the max. This code works on Nexus devices (eg. Galaxy Nexus) However, it doesn't work on Samsung devices (S3, S4, Note 2) [Android version 4.3].

Any suggestions?


回答1:


Since I couldn't find the answer, I came up with the work-around by actually playing the ringer sound in my service.



来源:https://stackoverflow.com/questions/23477865/setringermode-to-normall-during-incoming-call-doesnt-work-on-samsung-devices

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