3

I start the Voice recognition activity in a non activity class (by passing in the activity) here is the code:

private static void startVoiceRecognitionActivity() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Talk");
                myActivity.startActivityForResult(intent, REQUEST_CODE);
    }

last line myActivity is the activity i passed in to the class which has this method in.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (final EditText editText : editTextHandlingList) {
        if (requestCode == REQUEST_CODE && resultCode == theActivity.RESULT_OK) {
            ArrayList<String> results = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                        //log the result            }
    }
}

Now the problem is onActivityResult method. I want to be able to get the result back inside the same class and not in the activity. If it is vague please ask me questions..

As I pass the activity to this class is there any way that i can do this? There should be some way to handle this outside.. If you have any questions please ask me.

Vivere_FlowCoder
  • 277
  • 1
  • 4
  • 14

3 Answers3

1

I know this is an old question but with the help of new api its simple:

myActivity.registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        doSomeOperations()
    }
}.launch(yourIntent)

Saman Sattari
  • 3,322
  • 6
  • 30
  • 46
0
I want to be able to get the result back inside the same class and not 
in the activity.

No can do, you really cant get the result without executing your activity because startActivityForResult is bounded with your activity when the other activity is finished.

solution:

Since you have your myActivity in your another class you can use the Intent from it to get the result from another activity in your class where you started the VoiceRecognition. Also make sure that you call it after the VoiceRecognition activity is finished.

sample:

myActivity.getIntent().getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • I don't get what that means "you can use the Intent from it to get the result from another activity. " Could you elaborate more please? – Vivere_FlowCoder Aug 28 '14 at 20:14
  • @Vivere_FlowCoder lets say you already finised your `VoiceRecognition` activity since the intent of the activity has the information from it just call getIntent from your class – Rod_Algonquin Aug 28 '14 at 20:18
  • I used 'ArrayList results = myActivity.getIntent().getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);' right after 'myActivity.startActivityForResult(intent, REQUEST_CODE);', but I get nullpointerexception.. – Vivere_FlowCoder Aug 28 '14 at 20:38
  • @Vivere_FlowCoder no, make sure you call it after you go back in your activity. not after the `startActivity` – Rod_Algonquin Aug 28 '14 at 20:45
  • I know what you mean. But how can I call it after the VoiceRecognitionActivity is finished? or how can I call it after I go back to my activity? – Vivere_FlowCoder Aug 28 '14 at 20:57
  • I am starting the voicerecognitionactivity then I talk and I just want to log the voice to see what I said. what is this 'call it after you go back in your activity'? – Vivere_FlowCoder Aug 28 '14 at 21:05
  • @Vivere_FlowCoder then create an object of the class and in your `onActivityResult` call the method of the object of the class – Rod_Algonquin Aug 29 '14 at 01:50
  • where is this `onActivityResult` that you are talking about now? you know that I should have it inside the class and not the activity.. – Vivere_FlowCoder Aug 29 '14 at 07:46
0

If I understand the question, you are trying to do something like this:

Sometimes it is useful to create a utility class that encapsulates custom code (in this case voice-related). The utility class is just a bunch of static methods:

public static class VoiceRecognitionUtils {

    private static void startVoiceRecognitionActivity(Activity myActivity) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Talk");
                myActivity.startActivityForResult(intent, REQUEST_CODE);
    }

    private static void processResult(int requestCode, int resultCode, Intent data) {
        for (final EditText editText : editTextHandlingList) {
            if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
                ArrayList<String> results = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            }
        }
    }
}

Given that class your activity can just call the static methods from the appropriate lifecycle events:

public class MainActivity extends FragmentActivity {
    // ...

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        VoiceRecognitionUtils.processResult(requestCode, resultCode, data);
    }

    // call startVoiceRecognitionActivity() somewhere

}

The voice recognition is still being started and the results received by the Activity (because Android OS requires that), but the technical details are collected in the utility class.

I'm not certain from your question if this is what you were trying to do, but I would recommend this technique anyways, it's very useful.

x-code
  • 2,940
  • 1
  • 18
  • 19
  • no. As I mentioned I dont want to handle anything in the mainActivity. I Think you Didnot get my question. I want to do Everything inside the other class and I am passing the mainactivity to it to be able to start the voicerecognition activity – Vivere_FlowCoder Aug 28 '14 at 21:10
  • @Vivere_FlowCoder Got it. I interpreted the question in a way that allowed a useful answer. Since I was wrong it's unfortunately back to my original comment: you just can't do that! – x-code Aug 28 '14 at 21:15