我正在尝试使用提供的代码here在服务中运行语音识别API,但我遇到了麻烦。
我的活动有以下几行:
protected void onCreate(Bundle savedInstanceState) {
......
Intent startConstantSR = new Intent("MyService.class");
getApplicationContext().startService(startConstantSR);
....
}
并且服务的onCreate看起来像这样
public void onCreate()
{super.onCreate();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
我已经在onReadyForSpeech和onBeginningOfSpeech上设置了一个Toast,以便我知道我在正确的道路上 - 但没有任何一种吐司被解雇。
我做错了什么?我无法找到,因为我几乎不熟悉服务。
答案 0 :(得分:0)
您必须覆盖服务中的onStartCommand以发送侦听消息或绑定到服务并发送侦听消息
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Message msg = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
try
{
mServerMessenger.send(msg);
}
catch (RemoteException e)
{
}
return START_STICKY;
}