How to start the same activity using startActivityForResult twice - android

So I am trying to implement calling the same activity twice, I understand there will be better ways to do this but right now I just want 2 separate data recordings. When I try to run this code, the Diastolic BP gets read in first which is unintentional. Can someone explain why this is happening please. Thank you.
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//The following is required when ^^^ this is used
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Systolic Blood Pressure Value");
startActivityForResult(i, SYSTOLIC_CHECK);
//A different request code is required per activity called
Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
startActivityForResult(j, DIASTOLIC_CHECK);
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
processPictureWhenReady(picturePath);
// TODO: Show the thumbnail to the user while the full picture is being
// processed.
}
else if ((requestCode == SYSTOLIC_CHECK) && resultCode == RESULT_OK) {
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
System.out.println("Systolic BP: " + spokenText);
//OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));
}
else if ((requestCode == DIASTOLIC_CHECK) && resultCode == RESULT_OK) {
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
System.out.println("Diastolic BP: " + spokenText);
//OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));
}
super.onActivityResult(requestCode, resultCode, data);
}

The API simply doesn't work that way - you can't queue up the launch multiple activities as you are trying to do.
The normal way to do this is to launch the first Activity with startActivityForResult, and when the first activity returns, launch the second one.

If I'm not mistaken, since you call your Diastolic activity as last, this is laid over you systolic activity.
So you Diastolic Activity will be the one the user interacts with first.
I just want to point out that what #GreyBeardedGeek said is true.
It probably would be better if you just start you systolic activity, then, when you receive the result, start you Diastolic Activity (in OnActivityResult).
This will prevent issues with your activities not starting in the order you wanted.
EDIT
Your code would look something like this then
...else if ((requestCode == SYSTOLIC_CHECK) && resultCode == RESULT_OK) {
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
System.out.println("Systolic BP: " + spokenText);
//OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));
//call second activity
Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
startActivityForResult(j, DIASTOLIC_CHECK);
} ...
EDIT
Your activity could look like this.
public class VoiceActivity extends ActionBarActivity {
private String systole;
private String diastole;
private Button btnGetVoiceInput;
private final int SYSTOLIC_CHECK=1, DIASTOLIC_CHECK=2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnGetVoiceInput = (Button) findViewById(R.id.btnVoiceInput);
btnGetVoiceInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//The following is required when ^^^ this is used
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Systolic Blood Pressure Value");
startActivityForResult(i, SYSTOLIC_CHECK);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case SYSTOLIC_CHECK: {
if(resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
systole = results.get(0);
System.out.println("Systolic BP: " + systole);
Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
startActivityForResult(j, DIASTOLIC_CHECK);
}
break;
}
case DIASTOLIC_CHECK: {
if(resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
diastole = results.get(0);
Log.v("VoiceInput", "Diastolic BP: " + diastole);
doSomething();
}
break;
}
}
}
private void doSomething() {
//do what you want with both readings here
String bloodpressure = systole + " / " + diastole;
Log.v("Bloodpressure", bloodpressure);
}
}
After getting the 2 readings, you can do something with them.
This does still need some error handling, but I haven't used the voice input yet, so I don't know what values it returns/can return.

Related

Android studio, wait for one line of code to complete before starting next

Beginner question...
I have the following 4 lines of code.
The third line asks the user (with speech) whether he / she is certain about an action.
Then the fourth line calls a method called speechYesNo (included below) to listen for the users response.
The problem is that the speechYesNo method is called before the txtSpeech.speak line (3rd line) is finished speaking and it records the "Are you sure... " speech line as well as the users response.
For example, if the user responds "Yes", the result from the speechYesNo method will be something like... "Are you sure you would like to add xxxxx Yes".
I just want the result to be "yes".
Is there a way to force the third line (txtSpeech.speak) to finish before the speechYesNo() method is called?
Thanks
String strCommonName = myUtil.getCommonName(strFinalResult);
String strToSpeak = "Are you sure you would like to add " + strCommonName;
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);
speechYesNo();
Here is the speechYesNo() method
public void speechYesNo() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
if(intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 9);
} else {
Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
}
}
Here is the onActivityResult...
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 10:
if(resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String strResult = result.get(0);
Double dblStringComparison = 0.000;
String strFinalResult = "Unknown";
for(int intCount = 0; intCount <= arrAnimals.size()-1; intCount++) {
String strVal1 = strResult;
String strVal2 = myUtil.getCommonName(arrAnimals.get(intCount));
Double dblTemp = myUtil.similarity(strVal1, strVal2 );
if(dblTemp > dblStringComparison){
dblStringComparison = dblTemp;
strFinalResult = arrAnimals.get(intCount);
}
}
atvAnimalName.setText(strFinalResult);
String strCommonName = myUtil.getCommonName(strFinalResult);
String strToSpeak = "Are you sure you would like to add " + strCommonName;
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);
speechYesNo();
}
break;
case 9:
if(resultCode == RESULT_OK && data != null) {
ArrayList<String> result2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String strYesNo0 = result2.get(0);
Toast.makeText(context, strYesNo0, Toast.LENGTH_LONG).show();
}
break;
}
}
Here is the full script
The openSpeechMode method will fire when user clicks an image and will record the users speech selection.
Once this is done I want the app to ask the user is he / she is sure about his / her selection.
If the user responds "yes" then I will do some more work.
The problem as per above is the the speech recorder is recording the "are you sure " question as well as the "yes" response.
public void openSpeechMode(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
if(intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 10);
} else {
Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
}
}
public void speechYesNo() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
if(intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 9);
} else {
Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 10:
if(resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String strResult = result.get(0);
Double dblStringComparison = 0.000;
String strFinalResult = "Unknown";
for(int intCount = 0; intCount <= arrAnimals.size()-1; intCount++) {
String strVal1 = strResult;
String strVal2 = myUtil.getCommonName(arrAnimals.get(intCount));
Double dblTemp = myUtil.similarity(strVal1, strVal2 );
if(dblTemp > dblStringComparison){
dblStringComparison = dblTemp;
strFinalResult = arrAnimals.get(intCount);
}
}
atvAnimalName.setText(strFinalResult);
String strCommonName = myUtil.getCommonName(strFinalResult);
String strToSpeak = "Are you sure you would like to add " + strCommonName;
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);
// speechYesNo();
}
break;
case 9:
if(resultCode == RESULT_OK && data != null) {
ArrayList<String> result2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String strYesNo0 = result2.get(0);
Toast.makeText(context, strYesNo0, Toast.LENGTH_LONG).show();
}
break;
}
}
Question
The problem is that the speechYesNo method is called before the
txtSpeech.speak
Solution
txtSpeech seems to be TextToSpeech class. thus, you should use UtteranceProgressListener for resolving your problem.
Try as below code.
txtSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
}
#Override
public void onDone(String utteranceId) {
speechYesNo();
}
#Override
public void onError(String utteranceId) {
}
});
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);

My first project stuck on OnActivityforResult

I tried to make a simple app which can keep scores on a game with four players. When I press the plus or minus button it opens a new activity (dialog theme) where the user should enter their score. That score should be stored into a text view.
Some part of code looks like this:
Here I opened new activities
buttonPlus1.setOnClickListener(this);
buttonPlus2.setOnClickListener(this);
buttonPlus3.setOnClickListener(this);
buttonPlus4.setOnClickListener(this);
buttonMinus1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttPlusPlayer1:
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
break;
case R.id.buttPlusPlayer2:
Intent i2 = new Intent(this, ThirdPlusActivity.class);
startActivityForResult(i2, 2);
break;
case R.id.buttPlusPlayer3:
Intent i3 = new Intent(this, FourthPlusActivity.class);
startActivityForResult(i3, 3);
break;
case R.id.buttPlusPlayer4:
Intent i4 = new Intent(this, FifthPlusActivity.class);
startActivityForResult(i4, 4);
break;
case R.id.buttMinusPlayer1:
Intent i5 = new Intent(this, FirstMinusActivity.class);
startActivityForResult(i5, 1);
}
}
That's an example how activities send data back to main activity
public class SecondActivity extends AppCompatActivity {
private EditText mEditText;
private ImageButton mBackSpace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setTitle("ADD POINTS");
mEditText = findViewById(R.id.editTxtActivity);
//implementing backspace button
mBackSpace = findViewById(R.id.backSpaceButton);
mBackSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = mEditText.getText().toString();
if (str.length() > 0) {
str = str.substring(0, str.length() - 1);
mEditText.setText(str);
}
}
});
}
#Override
public void onBackPressed() {
Intent i = new Intent();
i.putExtra("message", mEditText.getText().toString());
setResult(RESULT_OK, i);
Toast.makeText(this, "You added " + mEditText.getText().toString() + " points", Toast.LENGTH_LONG).show();
finish();
}
}
and "now" I receive the results from activities
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String a = data.getStringExtra("message");
int i =Integer.parseInt(a);
String b = data.getStringExtra ("message5");
int z = Integer.parseInt(b);
Integer fResult = i - z;
String firstResult = fResult.toString();
mFirstScore.setText(firstResult);
}
if (requestCode == 2 && resultCode == RESULT_OK) {
mSecondscore.setText(data.getStringExtra("message2"));
}
if (requestCode == 3 && resultCode == RESULT_OK) {
mThirdScore.setText(data.getStringExtra("message3"));
}
if (requestCode == 4 && resultCode == RESULT_OK) {
mFourthscore.setText(data.getStringExtra("message4"));
}
the problem is on the last part of the code because I don't know how to receive data from activities and to calculate the result. I want when the user hit the plus button to add numbers and when minusButton is clicked to substract and the result to be stored on a TextView which, for player1, in my case is mFirstScore.
Is this possible or I did everything wrong and it is not possible to manipulate data received from another activity how I image
PS: on case buttMinusPlayer1 and on first if with requestCode 1 I've tried something ;
On SecondActivity put your key message2
#Override
public void onBackPressed() {
Intent i = new Intent();
i.putExtra("message2", mEditText.getText().toString());
setResult(RESULT_OK, i);
finish();
}
and get the data from SecondActivity by the same key message2
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2 && resultCode == RESULT_OK) {
mSecondscore.setText(data.getStringExtra("message2"));
}
}
Hope this helps.

Giving data to other activity not working actually

What is the code about:
With my code I can open a mic, speak into it and see the output.
Present Situation
Right now if I speak into the mic, I see the output and jump directly in other activity.
Requirement
I want take the output in the activity but before I text to much I show you the code.
Mainactivity.java
private TextView voiceInput;
private ImageView speakButton;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceInput = (TextView) findViewById(R.id.voiceInput);
speakButton = (ImageView) findViewById(R.id.btnSpeak);
speakButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
askSpeechInput();
}
});
}
// Showing google speech input dialog
private void askSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Sprechen Sie was ein");
//tent.putStringArrayListExtra("result",resultat);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
Intent intent=new Intent(this,Zweites.class);
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voiceInput.setText(result.get(0));
intent.putStringArrayListExtra("resultat",result);
//ActivityZweites wird gestartet
startActivity(intent);
}
break;
}
} }} <br>
In that I got the following problem:
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//Ausgabe in andere Activity
voiceInput.setText(result.get(0));
Intent intent=new Intent(this,Zweites.class);
intent.putStringArrayListExtra("resultat",result);
//ActivityZweites wird gestartet
startActivity(intent);
}
break;
}
There it outputs the spoken input:
voiceInput.setText(result.get(0));
And afterwards it jumps into the activity.
How can I get this output in the activity without first display the output and jump in another activity?
I tried some things but donĀ“t come to a solution how to fix this problem.
Do you guys got some ideas?
Kind regards
If you simply want to pass the same text to your intent that you're displaying in your TextView, exchange
intent.putStringArrayListExtra("resultat",result);
with
intent.putExtra("resultat", result.get(0));
Then, in your Zweites activity, you can get the string like so:
Intent intent = getIntent();
String text = intent.getStringExtra("resultat");

How to handle getResults from multiple activities?

I am trying to get results back from intents in android studio.
In my main I start an activity and use startActivityForResult(intent, 1)
I then use get results in mainActivity from activity 2's setResults()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
if (extras != null) {
String name = extras.getString("FIRSTNAME");
String Lname = extras.getString("LASTNAME");
int ID = extras.getInt("ID");
//TODO: Get the list fragment to newinstance with out new arraylist
Person p = new Person(name, Lname, ID);
people.add(p);
getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
}
In my fragment in activity 1 I am calling a new startActivityForResult(i, 2)
How do i get my main activity to grab the setResults()
from Activity 3?
Activity 3 is doing this:
Intent deleteIntent = new Intent();
deleteIntent.putExtra("FNAME", first);
deleteIntent.putExtra("LNAME", last);
deleteIntent.putExtra("ID", num);
setResult(RESULT_OK, deleteIntent);
finish();
I am trying to have my main activity call if (requestCode == 2)
But it works to no avail.
Here is the all the onActivityResult for reference:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
if (extras != null) {
String name = extras.getString("FIRSTNAME");
String Lname = extras.getString("LASTNAME");
int ID = extras.getInt("ID");
//TODO: Get the list fragment to newinstance with out new arraylist
Person p = new Person(name, Lname, ID);
people.add(p);
getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
}
// NOW SEEING IF THE DETAILS SCREEN PASSED BACK RESULTS
} else if (requestCode == 2) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null) {
String name = extras.getString("FNAME");
String Lname = extras.getString("LNAME");
int ID = extras.getInt("ID");
Person p = new Person(name, Lname, ID);
// Delete happens here //
if (people.contains(p)) {
people.remove(p);
// If empty show blank frag, if not, update list //
if (people.isEmpty()) {
getFragmentManager().beginTransaction().replace(R.id.content_main, BlankList.newInstance());
} else {
getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
}
} else {
Toast.makeText(this, "DIDNT RECEIVE SAME INFO", Toast.LENGTH_SHORT).show();
}
}
}
}
// END ELSE CHECK
}
}
Here is the code that is calling the startActivityForResult() in the Fragment on activity 1.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);
ArrayList<Person> people = (ArrayList<Person>) getArguments().getSerializable(ARG_People);
if (people != null && position != -1) {
Person listPerson = people.get(position);
Intent i = new Intent("OPENDETAILS");
i.putExtra("NAME", listPerson.name);
i.putExtra("LASTNAME", listPerson.lName);
i.putExtra("ID", listPerson.ID);
startActivityForResult(i, 2);
} else {
Toast.makeText(getActivity(), "EMPTY LIST ERROR", Toast.LENGTH_SHORT).show();
}
}
It's a little unclear what you're trying to do, but it sounds like:
Activity1 starts Activity2 for result
Activity2 starts Activity3 for result
Activity3 returns a result
Activity1 is expected receive Activity3's result.
If I got that right then the key element that seems to be missing here is that you are expecting Activity1 to get a result from Activity3 even though it was Activity2 that started it for result. In this case you should implement onActivityResult in Activity2, handle the results coming back from Activity3 and set them as Activity2's results to pass back to Activity1 and then finish; An activity will only receive results from activities it directly starts via startActivityForResult.
Use different code to launch different activities,
such as
startActivtityForResult(new Intent(this,Person1.class),1);
startActivtityForResult(new Intent(this,Person2.class),2);
startActivtityForResult(new Intent(this,Person3.class),3);
then on activityresult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 1:
//implement your code here
break;
case 2:
//implement your code here
break;
case 3:
//implement your code here
break;
}
}
then set Retrun result in these classes
Person1.class
return_intent.putExtra("result", 1);
setResult(1, return_intent);
Person2.class
Person1.class
return_intent.putExtra("result", 2);
setResult(2, return_intent);
Person3.class
Person1.class
return_intent.putExtra("result", 3);
setResult(3, return_intent);

Taking the first data of the results in voice recognition

In my project i will use voice recognition. In the codes of voice recognition, an arraylist returns on activity result. But i just want to take the first data in the list. i mean in the project users will use speak button, speak and then see the results. And then there will be continue button that shows the first data of the results. How can i pass the result parameter to another activity class. But not as the originally list, just a string.
I use this code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> matches = new ArrayList<String>();
matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(matches.lenght()>0) {
result = matches.get(0)
//Time to create the new intent to the other activity
Intent i = new Intent (Activity1.this, Activity2.class);
i.putExtra("RESULT", result); //RESULT is the key
startActivityForResult(i,ID); // or startActivity(i)
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Then in the other activity you must to uget the bundle and extrat the string
Bundle bundle = getIntent().getExtras();
if( (bundle!=null) && (bundle.contains("RESULT"){
String text = bundle.getString("RESULT");
}
else{
//other things
return;
}

Categories

Resources