0

我已经制作了自定义拨号器,并且我已经在按钮的按下事件中设置了音调。现在问题是我已经设置了此时的音量是固定的,所以当手机处于静音模式时,也会播放应用程序音调。所以我想根据手机的拨号键音量进行自定义。自定义拨号器的代码如下:

public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keypad1);
        ctx = this;



        b1 = (ImageButton) findViewById(R.id.imageButton1);
        b2 = (ImageButton) findViewById(R.id.imageButton2);
        b3 = (ImageButton) findViewById(R.id.imageButton3);
        b4 = (ImageButton) findViewById(R.id.imageButton4);
        b5 = (ImageButton) findViewById(R.id.imageButton5);
        b6 = (ImageButton) findViewById(R.id.imageButton6);
        b7 = (ImageButton) findViewById(R.id.imageButton7);
        b8 = (ImageButton) findViewById(R.id.imageButton8);
        b9 = (ImageButton) findViewById(R.id.imageButton9);
        b10 = (ImageButton) findViewById(R.id.imageButtonstar);
        b11 = (ImageButton) findViewById(R.id.imageButton0);
        b12 = (ImageButton) findViewById(R.id.imageButtonhash);

        call = (ImageButton) findViewById(R.id.imageButtoncall);
        back = (ImageButton) findViewById(R.id.back);
        logout = (ImageButton) findViewById(R.id.imageButtonlogout);
        keypad = (ImageButton) findViewById(R.id.imageButtondialpad);
        txt = (EditText) findViewById(R.id.txtnum);
        /*AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int streamMaxVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        Log.i("volume",""+streamMaxVolume);*/
        mp1 = new ToneGenerator(AudioManager.STREAM_DTMF,70);

        txt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                txt.setSelection(txt.getText().length());
            }
        });



        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = txt.getText().toString();
                txt.setText(s + "1");
                mp1.stopTone();
                mp1.startTone(ToneGenerator.TONE_DTMF_1, 120);
            }
        });

    b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = txt.getText().toString();


                txt.setText(s + "2");
                mp1.stopTone();
                mp1.startTone(ToneGenerator.TONE_DTMF_2, 120);

            }
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = txt.getText().toString();
                txt.setText(s + "3");
                mp1.stopTone();
                mp1.startTone(ToneGenerator.TONE_DTMF_3, 120);

            }
        });
        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = txt.getText().toString();
                txt.setText(s + "4");
                mp1.stopTone();
                mp1.startTone(ToneGenerator.TONE_DTMF_4, 120);

            }
        });
        b5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = txt.getText().toString();
                txt.setText(s + "5");
                mp1.stopTone();
                mp1.startTone(ToneGenerator.TONE_DTMF_5, 120);

            }
        });
4

1 回答 1

2

一种建议的解决方案可能是,如果您不想在静音模式下播放按键音,那么您必须检测电话铃声模式天气是否静音

        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = audioManager.getRingerMode();

        if ((ringerMode == AudioManager.RINGER_MODE_SILENT) || (ringerMode == AudioManager.RINGER_MODE_VIBRATE))
        {
            // do nothing
        }else {

            // do something
        }
于 2016-04-13T08:41:16.347 回答