Search code examples
javaandroidtextmethodsspeech

Android Speech to text API Getting variable value out of onActivityResult method


Hi How can I use Android Speech to text API to get a value out of onActivityResult and use it in other activities/methods?

Heres the example code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub


    if(requestCode == check && resultCode == RESULT_OK){

         String results1 = data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
         EditText test = (EditText) findViewById(R.id.editText1);
         test.setText(results1);
    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        ListView lv = (ListView) findViewById(R.id.listView);
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
        whatYouSaid = results.get(0); }

So yeah.... How can i get this string value of whatYouSaid from this method to be able to use it in ohter methods/classes?


Solution

    1. Store it as a global static variable in your app.
    2. Save this in Shared preferences.
    3. If its just few more activities you can pass it through Intent;

    4. SharedPreference:

    starting from this line

    .......
    whatYouSaid = results.get(0);
    
     Sharedpreferences sp = getSharedPreferences("UR_UNIQ_PREF_ID", Context.MODE_PRIVATE);
      Editor editor = sp.edit();
      editor.putString("WHATYOUSAID", whatyousaid);
     editor.commit();
    
    //Then start your another activity
    
    //Then in your next activity
    
    oncreate(){
    
    ......
     Sharedpreferences sp = getSharedPreferences("UR_UNIQ_PREF_ID", Context.MODE_PRIVATE);
     String whatyousaid =  sp.getString("UR_UNIQ_PREF_ID","");
    
    }
    

    }