在来电期间,用户可以将手机铃声设为静音,但在 Android 手机中按下音量调高或开机按钮。我正在创建一个说来电者姓名的应用程序,在我的应用程序中将说出来电者的姓名。我没有将当前铃声静音。我的应用程序一切正常,除非电话在来电期间响铃并且用户通过按音量或电源按钮将其设为静音。我无法处理这个触发器。我尝试了 AudioManager 和 RingToneManager 类但徒劳无功。我的应用程序有一个活动(应用程序设置)、一个广播接收器类(用于捕获来电)和一个服务(用于触发 TTS 引擎)。
代码 :
private void SpeakCallerName(String txtSpeak) {
Runnable runnable = new Runnable() {
@Override
public void run() {
Ringtone ringtone = null;
_status = "SpeakService speak Runnable phoneRinging: " + phoneRinging;
Log.i("Test", _status);
int i =0;
try {
while(phoneRinging) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
_status = "SpeakService speak Runnable inside while loop count: "+i + "phoneRinging:" + phoneRinging;
Log.i("Test", _status);
int Mode = audioManager.getMode();
_status = "SpeakService speak Runnable inside while loop count: "+i + ":audioManager.getMode():" + Mode;
Log.i("Test", _status);
int ringerMode=audioManager.getRingerMode();
_status = "SpeakService speak Runnable inside while loop count: "+i + ":audioManager.getRingerMode():" + ringerMode;
Log.i("Test", _status);
int StreamVolume=audioManager.getStreamVolume(AudioManager.STREAM_RING);
_status = "SpeakService speak Runnable inside while loop count: "+i + ":audioManager.getStreamVolume:" + StreamVolume;
Log.i("Test", _status);
myTts.speak(_txtSpeak,TextToSpeech.QUEUE_FLUSH,null);
Thread.sleep(5000);
_status = "SpeakService speak Runnable count: "+i + " slept for 8 sec";
Log.i("Test", _status);
i+=1;
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (Exception e) {
_status = "SpeakService speak Runnable count: "+i + " EXCEPTION: " + e.getMessage();
Log.i("Test", _status);
}
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
我的日志是这样的:
SpeakService 在 while 循环计数:1phoneRinging:true SpeakService 在 while 循环计数:1:audioManager.getMode():1 SpeakService 说 Runnable 在 while 循环计数:1:audioManager.getRingerMode():2 SpeakService 在 while 内部说 Runnable循环计数:1:audioManager.getStreamVolume:7
当用户按下音量或电源按钮时,我仍然会在日志中获得上述值。