カスタムダイヤラを作成し、ボタンのプレスイベントでトーンを設定しました。問題は、この時点で音量が固定されているため、電話が無音モードのときにアプリケーショントーンからも再生されることです。だから私は電話のダイヤラキーの音量に基づいてカスタマイズしたいと思います。カスタムダイヤラのコードは以下のとおりです。
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);
}
});