Note that there are some explanatory texts on larger screens.

plurals
  1. POOnActivityResult handling different requestCodes within same activity
    primarykey
    data
    text
    <p>Been trying to apply some of the solutions from similar questions, but I've had no luck so far.</p> <p>I have an activity with two buttons, each button calls the RecognizerIntent to do some speech recognition. Im trying to have each button call its own RecognizerIntent and check for separate requestCode(s) with in the same activity.</p> <p>this is what I have so far:</p> <pre><code>public class Cooking extends Activity implements OnClickListener{ ListView lv; static final int check = 111; static final int checka = 111; static final int checkb = 111; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.cooking); initialize(); } private void initialize() { // TODO Auto-generated method stub lv = (ListView)findViewById(R.id.lvVoiceReturn); display = (ImageView) findViewById(R.id.IVdisplay); ImageView image1 = (ImageView) findViewById(R.id.IVimage1); ImageView image2 = (ImageView) findViewById(R.id.IVimage2); image1.setOnClickListener(this); image2.setOnClickListener(this); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(false); } @Override public void onClick(View v) { if(v.getId() == R.id.IVimage1){ Intent a = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); a.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); a.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); a.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak."); startActivityForResult(a, checka); }else if(v.getId() == R.id.IVimage2){ Intent b = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); b.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); b.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); b.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak."); startActivityForResult(b, checkb); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (requestCode == checka &amp;&amp; resultCode == RESULT_OK){ ArrayList&lt;String&gt; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); lv.setAdapter( new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, results)); if(results.contains("really") || results.contains("yes really") || results.contains("disease")) { /////toast referenced to xml the after 400ms counter_score ++ ; txView.setId(1); counter_score_z +=1; ourSong = MediaPlayer.create(Cooking.this, R.raw.rightsound2); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Nice"); dialogBuilder.setMessage("Correct!."); dialogBuilder.setIcon(R.drawable.ic_mark); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(2500); }catch (InterruptedException e){ e.printStackTrace(); } finally { Intent nextAct = new Intent(Cooking.this, LetterEx.class); startActivity(nextAct); finish(); } } }; timer.start(); }else{ lv.setAdapter( new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, results)); counter_score +=0; txView.setId(0); counter_score_z +=0; ourSong = MediaPlayer.create(Cooking.this, R.raw.wrongsound); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Oops"); dialogBuilder.setMessage("Try again"); dialogBuilder.setIcon(R.drawable.ic_wrong); dialogBuilder.setButton( "Continue", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent openApp = new Intent(Cooking.this, LetterEx.class); startActivity(openApp); finish(); } }); dialogBuilder.setButton2( "try again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(4000); }catch (InterruptedException e){ e.printStackTrace(); } finally { } } }; timer.start(); } } if (requestCode == checkb &amp;&amp; resultCode == RESULT_OK){ ArrayList&lt;String&gt; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); lv.setAdapter( new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, results)); if(results.contains("delivering mail") || results.contains("no not really") || results.contains("hi")) { /////toast referenced to xml the after 400ms counter_score ++ ; txView.setId(1); counter_score_z +=1; ourSong = MediaPlayer.create(Cooking.this, R.raw.rightsound2); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Nice");dialogBuilder.setMessage("Correct!"); dialogBuilder.setIcon(R.drawable.ic_mark); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(2500); }catch (InterruptedException e){ e.printStackTrace(); } finally { Intent nextAct2 = new Intent(Cooking.this, LetterEx.class); startActivity(nextAct2); finish(); } } }; timer.start(); }else{ lv.setAdapter( new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, results)); counter_score +=0; txView.setId(0); counter_score_z +=0; ourSong = MediaPlayer.create(Cooking.this, R.raw.wrongsound); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Oops"); dialogBuilder.setMessage("Try again"); dialogBuilder.setIcon(R.drawable.ic_wrong); dialogBuilder.setButton( "Continue", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent openApp2 = new Intent(Cooking.this, LetterEx.class); startActivity(openApp2); finish(); } }); dialogBuilder.setButton2("try again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(4000); }catch (InterruptedException e){ e.printStackTrace(); } finally { } } }; timer.start(); } } super.onActivityResult(requestCode, resultCode, data); } } </code></pre> <p>This doesnt crash or anything, but its not doing what I want it to do.</p> <p>When button (IVimage1) is pressed...</p> <pre><code>public void onClick(View v) { if(v.getId() == R.id.IVimage1){ Intent a = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); a.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); a.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); a.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak."); startActivityForResult(a, checka); </code></pre> <p>The requestCode for checka is correctly checked and the "correct" dialog shows up, however the incorrect dialog for "checkb" also shows up. Clearly I need to break the If statement code..to separate the requestCodes and assing one to each button.</p> <p>I've tried break; return; doing swtich(); and case from similar questions but have had no luck so far...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload