setRingerMode during incoming call - RINGING

孤者浪人 提交于 2019-12-05 09:59:47

问题


I have a task - change the ringer volume immediately when phone ringing. For example: After detecting, that there is incoming call I need to set ringer volume to 0 (mute) and vibrator also should be disabled (if it not disabled already). Then there is delay when I need to perform another code (startComputing();). After that ringer volume should be changed to certain value (f.e.7) and vibrator should be activated. Here is my code:

    public class IncomingCallReceiver extends BroadcastReceiver {
        private AudioManager amanager;
        public void onReceive(Context context, Intent intent) {
            if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                amanager.setRingerMode(0x00000000); // no sound and no vibration            
                startComputing();
                amanager.setRingerMode(0x00000002); // normal
                amanager.setStreamVolume(AudioManager.STREAM_RING, 7,
                        AudioManager.FLAG_SHOW_UI
                                + AudioManager.FLAG_PLAY_SOUND);
            }

        }
        private void startComputing() {
            try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
        }

}

1)The main problem is that after this: amanager.setStreamVolume(AudioManager.STREAM_RING, 7, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND); ringer does not ring at all, only toast message appears that sound level changed but phone does not play any sound. How this can be changed? 2)Also there is problem that amanager.setRingerMode(0x00000000); does not change Volume and vibration immediately but just in about half a sec. Thank You in Advance. Jacob


回答1:


I don't think there is anything you can do to overcome the 1/2 sec delay before you mute the ringer. Sometimes it works (ie very short or no delay), sometimes you will hear the ringer for a short period of time.

I think the problem you are having is with the flags passed to setStreamVolume.

Try this:

amanager.setStreamVolume(AudioManager.STREAM_RING, 7,
    AudioManager.FLAG_SHOW_UI|AudioManager.FLAG_PLAY_SOUND);

You should be using the bitwise inclusive or (|), and not simple addition (+).



来源:https://stackoverflow.com/questions/8175650/setringermode-during-incoming-call-ringing

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