How to get the result from OnActivityResult inside another class?(outside of the activity

后端 未结 2 450
栀梦
栀梦 2021-01-22 11:25

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

private static void startVoiceRecognitionActivity(         


        
相关标签:
2条回答
  • 2021-01-22 11:58

    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.

    0 讨论(0)
  • 2021-01-22 12:00
    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);
    
    0 讨论(0)
提交回复
热议问题