我正在尝试制作一个应用程序,当使用android tts打电话到手机时,它会说出呼叫者的联系人姓名(你想参加吗?)根据用户的回复,选择接听或挂断。其中我总是得到一个问题与语音识别(显示未知的问题)在这里我给我的代码,可以有人帮助我解决这个问题。几天前我突然想到这个问题,请您提供帮助,我将不胜感激。
public class myPhoneStateChangeListener extends PhoneStateListener
{
speechcontact clsspcntct = new speechcontact();
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_RINGING)
{
String phoneNumber = incomingNumber;
String ContactName = objUtility.getContactName2(context,phoneNumber);
mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
speakWords(ContactName);
}
if(state == TelephonyManager.CALL_STATE_IDLE && ph_state==1)
{
mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
}
}
@Override
public void onInit(int initStatus)
{
if (initStatus == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.ENGLISH)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.ENGLISH);
}
else if (initStatus == TextToSpeech.ERROR) {
Log.d("speech log", "Sorry! Text To Speech failed...");
}
}
public void speakWords(String speech)
{
myTTS.speak("you have call from"+speech+"do you want to attend",TextToSpeech.QUEUE_FLUSH, null);
new Timer().schedule(new TimerTask() {
@Override
public void run()
{
startVoiceRecognitionActivity();
}
}, 5000);
}
public void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
try
{
startActivityForResult(intent, REQUEST_CODE);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(getApplicationContext(),"Ops! Your device doesn't support Speech to Text",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
String spch = data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
if (spch.contains("Yes"))
{
// do smthing
}
else if(spch.contains("No"))
{
// do smething
}
}
super.onActivityResult(requestCode, resultCode, data);
}
问题是,您正在添加FLAG_ACTIVITY_NEW_TASK到您的意图,根据文档:
当调用者从正在启动的活动请求结果时,不能使用此标志。
和startActivityForResult
一起使用,删除
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
我有一个应用程序,做你想要的,它在2.3到4.0的工作很好。在软糖上不工作。谷歌做出了改变,看起来像果冻豆停止话机振铃时的语音识别。
所以如果你在Jelly bean上测试,请在其他设备上测试你的代码。