I have an activity here. When the user touches the layout to the activity, I want the app to ask the user to give five pieces of information. In order to accomplish this, a series of five voice input prompts come up. Below, I have the code for this:
package com.example.shivamgandhi.gyrosafe;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.Locale;
public class Memory_Test1_Activity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {
EditText ed23, ed24, ed25, ed26, ed27;
private final int REQ_CODE_SPEECH_INPUT_TOWN = 100;
private final int REQ_CODE_SPEECH_INPUT_WIN = 101;
private final int REQ_CODE_SPEECH_INPUT_MONTH = 102;
private final int REQ_CODE_SPEECH_INPUT_DAY = 103;
private final int REQ_CODE_SPEECH_INPUT_TEAM = 104;
int n = 1;
Button btnarray[] = new Button[n];
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedpreferences;
RelativeLayout RelativeLayout;
int count = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memory_test1);
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
btnarray[0] = (Button)findViewById(R.id.button8);
sharedpreferences = this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
ed23 = (EditText)findViewById(R.id.editText23);
ed24 = (EditText)findViewById(R.id.editText24);
ed25 = (EditText)findViewById(R.id.editText25);
ed26 = (EditText)findViewById(R.id.editText26);
ed27 = (EditText)findViewById(R.id.editText27);
RelativeLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
for(int i = 0; i <n; i++){
btnarray[i].setOnClickListener(this);
}
RelativeLayout.setOnTouchListener(this);
}
private void promptSpeechInput_town() {
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TOWN);
}
catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
private void promptSpeechInput_win() {
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_WIN);
}
catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
private void promptSpeechInput_month() {
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_MONTH);
}
catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
private void promptSpeechInput_day() {
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_DAY);
}
catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
private void promptSpeechInput_team() {
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TEAM);
}
catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT_TOWN: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_twn = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed23.setText(result_twn.get(0));
promptSpeechInput_win();
}
}
case REQ_CODE_SPEECH_INPUT_DAY:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_day = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed26.setText(result_day.get(0));
promptSpeechInput_team();
}
}
case REQ_CODE_SPEECH_INPUT_WIN:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_win = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed24.setText(result_win.get(0));
promptSpeechInput_month();
}
}
case REQ_CODE_SPEECH_INPUT_MONTH:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_month = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed25.setText(result_month.get(0));
promptSpeechInput_day();
}
}
case REQ_CODE_SPEECH_INPUT_TEAM:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_team = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed27.setText(result_team.get(0));
}
}
}
}
@Override
public boolean onTouch(View v, MotionEvent event){
if(v == RelativeLayout && count == 0 ){
promptSpeechInput_town();
count = 1;
return true;
}
else{
return false;
}
}
The problem I am facing is that when I touch the layout, only one of the speech prompts happen, promptSpeechInput_team. How can I make it so that each of the prompts is called?
Edit: I now have each function in OnActivityResult calling eachother. However, I still have the voice input keep going indefinitely.
You need to call promptSpeechInput_win()
in onActivityResult of promptSpeechInput_town
and so on.. Ideally only one voice input can be acquired from the user. Hence you should be initiating the next one onActivityResult of previous voice request.
Also you need be breaking the switch case to avoid all the cases being executed every time.