当我在android中使用Speech Recognizer Intent时。 intent永远不会调用OnActivityResult方法。调试器永远不会在方法的第一行捕获它。此示例从示例here进行了修改,以合并Xamarin Forms。
private const int Voice = 10;
public void RecordSpeech()
{
Intent voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
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);
((Activity)Forms.Context).StartActivityForResult(voiceIntent, Voice);
//This doesn't work either
//((Activity)Forms.Context).StartActivityForResult(Intent.CreateChooser(new Intent(voiceIntent), "voiceIntent"), Voice);
}
protected override void OnActivityResult(int requestCode, Result resultVal, Intent data)
{ //A break point here doesn't work
Debug.WriteLine("Debug");
if (requestCode == Voice)
{
if (resultVal == Result.Ok)
{
IList<string> matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
if (matches.Count != 0)
{
Debug.WriteLine(matches[0]);
}
}
}
base.OnActivityResult(requestCode, resultVal, data);
}
答案 0 :(得分:3)
OnActivityResult需要位于MainActivity类中。把它放在那里固定一切。