Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to return value using StartActivityForResult

glowa500glowa500 PLMember
edited October 2016 in Xamarin.Android

I'm trying to use speech recognition in my app. For first time I'm using .Droid project in my SharedProject. I created interface ISpeechToText in shared project and cSpeechToText::Activity, ISpeechToText class in .Droid . My question is how to return value in ConvertSpeechToText function where I'm calling StartActivityForResult? I know that result is generated in OnActiveResult Event, but how to return it in ConvertSpeechToText function? Below I put pseudocode where I'm showing what I need to do.


   public class cSpeechToTech : Activity, ISpeechToText {

      public string ConverSpeechToText() {

          var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
          voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);

          voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, "Mów");

          voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1500);
          voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1500);
          voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 15000);
          voiceIntent.PutExtra(RecognizerIntent.ExtraMaxResults, 1);

          voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);
          StartActivityForResult(voiceIntent, VOICE);



           protected override void OnActivityResult(int requestCode, Result resultVal, Intent data) {

          if(requestCode == VOICE) {
            if(resultVal == Result.Ok) {
              var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
              if(matches.Count != 0) {
                string textInput = textBox.Text + matches[0];

                // limit the output to 500 characters
                if(textInput.Length > 500)
                  textInput = textInput.Substring(0, 500);
                textBox.Text = textInput;
              } else
                textBox.Text = "No speech was recognised";


          base.OnActivityResult(requestCode, resultVal, data);



Thanks for help!


  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    Have a look at TaskCompletionSource - this allow you to wait till a result is set on a task, and you can set this result in the OnActivityResult method.

Sign In or Register to comment.