2012-08-25 144 views
2

一直试图应用来自类似问题的一些解决方案,但迄今为止我还没有运气。OnActivityResult处理同一活动中的不同请求代码

我有两个按钮的活动,每个按钮调用RecognizerIntent做一些语音识别。 我试图让每个按钮都调用自己的RecognizerIntent,并在同一个活动中检查单独的requestCode(s)。

这是我到目前为止有:

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 && resultCode == RESULT_OK){ 
      ArrayList<String> results = 
       data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      lv.setAdapter(
       new ArrayAdapter<String>(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<String>(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 && resultCode == RESULT_OK){ 
      ArrayList<String> results = 
       data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      lv.setAdapter(
       new ArrayAdapter<String>(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<String>(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); 
     } 
    } 

这并不崩溃或任何东西,但它不是做我想做的事情。

当按下按钮(IVimage1)...

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); 

为checka的requestCode正确检查和“正确”的对话框中显示出来,但是,对于“checkb”不正确的对话框中也出现了。很明显,我需要打破If语句代码来分隔requestCodes并将其分配给每个按钮。

我试过了;返回;做swtich();并从类似的问题,但迄今没有运气的情况下...

回答

1

你已经定义它,使checka == checkb总是。尝试给他们不同的价值。

// For example: 
    static final int check = 111; 
    static final int checka = 112; 
    static final int checkb = 113; 
+0

是的..这是它..谢谢! – Mcorv

相关问题