I copied this from internet, other people say that works, but when I try to speak it doesn't do anything.
I tried to write the code about the intent outside the method onBeginingOfSpeech() and it works.
public class Schermata extends ActionBarActivity implements RecognitionListener{
SpeechRecognizer speechRecognizer;
TextView test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schermata);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
test = (TextView) findViewById(R.id.test);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_schermata, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onReadyForSpeech(Bundle params) {
test.setText("INIZIO");
}
@Override
public void onBeginningOfSpeech() {
test.setText("INIZIO");
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizer.startListening(intent);
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
test.setText("FINE");
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
ArrayList<String> res = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String s = "";
for (String result : res)
s += result + "\n";
test.setText(s);
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
}
Ensure you have RECORD_AUDIO permission in the manifest, voice recognition doesn`t work without it.