In general, I did what I asked in the previous question.

public class VoiceActivity extends Activity implements OnClickListener { private TextView mText; private SpeechRecognizer sr; private static final String TAG = "MyActivity"; public String str; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button speakButton = (Button) findViewById(R.id.speakButton); mText = (TextView) findViewById(R.id.textView1); speakButton.setOnClickListener(this); sr = SpeechRecognizer.createSpeechRecognizer(this); sr.setRecognitionListener(new listener()); } class listener implements RecognitionListener { public void onReadyForSpeech(Bundle params) { Log.d(TAG, "onReadyForSpeech"); } public void onBeginningOfSpeech() { Log.d(TAG, "onBeginningOfSpeech"); } public void onRmsChanged(float rmsdB) { Log.d(TAG, "onRmsChanged"); } public void onBufferReceived(byte[] buffer) { Log.d(TAG, "onBufferReceived"); } public void onEndOfSpeech() { Log.d(TAG, "onEndofSpeech"); } public void onError(int error) { Log.d(TAG, "error " + error); mText.setText("error " + error); } public void onResults(Bundle results) { str = new String(); Log.d(TAG, "onResults " + results); ArrayList<String> data = results .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); for (int i = 0; i < data.size(); i++) { Log.d(TAG, "result " + data.get(i)); str += data.get(i); } mText.setText("results: " + str); } public void onPartialResults(Bundle partialResults) { Log.d(TAG, "onPartialResults"); } public void onEvent(int eventType, Bundle params) { Log.d(TAG, "onEvent " + eventType); } } public void onClick(View v) { if (v.getId() == R.id.speakButton) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.moc"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); sr.startListening(intent); } } } 

I am interested in the following question: how to correctly forward the received string to another Activity, without outputting it to TextViev?


Update: But how to make an automatic transition to another activity after determining the voice (click - say - opens the next. Activation and the result in it)? Simply, if like in my example, he writes an error in the intent.setClass string (this, SecondActivity.class);

There was a thought:

 public void onResults(Bundle results) { str = new String(); Log.d(TAG, "onResults " + results); ArrayList<String> data = results .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); for (int i = 0; i < data.size(); i++) { Log.d(TAG, "result " + data.get(i)); str += data.get(i); } Intent intent = new Intent(); Bundle b = new Bundle(); b.putString("StrID", str); intent.putExtras(b); intent.setClass(this, SecondActivity.class); startActivity(intent); } 

but it is extremely wrong.

  • Update response: intent.setClass (VoiceActivity.this, SecondActivity.class) - tim_taller

1 answer 1

Well, I would not say that the thought with an intent is incorrect - extras are just meant for transferring data between activations.

Only I would have reduced your activation code a little, like this:

 Intent intent = new Intent(this, SecondActivity.class); intent.putExtras("StrID", str); startActivity(intent); 

But this is the case if you only need the string in SecondActivity. If it is needed later in some kind of activation, then it will be boring to send it back and forth.

In this case, you can create your own application class ( documentation ):

 public class MyApp extends Application { public String recognizedString; //объявлена public для примера, можно сделать private и геттеры/сеттеры для неё } 

Access to the application instance can be obtained from any activity.

In VoiceActivity:

 ... MyApp app = (MyApp) getApplication(); app.recognizedString = str; ... 

In SecondActivity:

 ... MyApp app = (MyApp) getApplication(); String recStr = app.recognizedString; ... 
  • Thank you, the transfer is very clear and detailed. - tim_taller