1
我創建此服務在後臺監聽,職業服務,但不知道監聽器是否收聽,但只在服務啓動時在Logcat消息中顯示...我已閱讀其他問題在stackoberflow中,但任何運行在我的情況。識別監聽不聽
public class ServicioVoz extends RecognitionService {
private SpeechRecognizer sr;
private VoiceResultsListener vrl;
public Principal principal;
@Override
public void onCreate() {
super.onCreate();
Log.i("Dins del servei", "Arrancat");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("SimpleVoiceService", "Service stopped");
}
@Override
protected void onCancel(Callback listener) {
sr.cancel();
}
@Override
protected void onStartListening(Intent recognizerIntent, Callback listener) {
Log.e("escoltant", "________________________________");
sr.setRecognitionListener(new VoiceResultsListener(listener));
sr.startListening(recognizerIntent);
}
@Override
protected void onStopListening(Callback listener) {
sr.stopListening();
Log.e("Atura d'escoltar", "________________________________");
}
/* INICIALIZAMOS UNA NUEVA CLASE*/
private class VoiceResultsListener implements RecognitionListener {
private Callback UserListener;
public VoiceResultsListener(Callback userSpecifiedListener) {
UserListener = userSpecifiedListener;
}
@Override
public void onBeginningOfSpeech() {
try {
UserListener.beginningOfSpeech();
} catch (RemoteException e) {
e.printStackTrace();
Log.e("ERROR", "No comença ha escoltar");
}
}
@Override
public void onBufferReceived(byte[] buffer) {
try {
UserListener.bufferReceived(buffer);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onEndOfSpeech() {
try {
UserListener.endOfSpeech();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onError(int error) {
try {
UserListener.error(error);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onEvent(int eventType, Bundle params) {
;
}
@Override
public void onPartialResults(Bundle partialResults) {
try {
UserListener.partialResults(partialResults);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onReadyForSpeech(Bundle params) {
try {
UserListener.readyForSpeech(params);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onResults(Bundle results) {
Log.e("resultats", "nose que possar");
try {
UserListener.results(results);
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String result : matches)
text += result + "\n";
Log.e("", text);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onRmsChanged(float rmsdB) {
try {
UserListener.rmsChanged(rmsdB);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
謝謝,但我想在一個服務。 – Gannon