2013-08-05 100 views
0

我正尝试在提供的代码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成立了吐司展现出来,所以我知道我在正确的道路上 - 但没有烤面包。

我在做什么错?我找不到,因为我几乎不了解服务。

回答

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; 
} 

对于绑定实现看Android Speech Recognition Continuous Service

+0

道具编写整个代码! –