All you need to know: I have a dialog with a button in it. When the button is pressed I want to start the speech recognition in my MainActivity. (The dialog is created by another class, I handle the clicks via an interface).
So here is the relevant code: (in MainActivity)
public void speechToText(boolean isName) {
this.isName = isName;
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString());
//intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.prompt));
try {
startActivityForResult(intent, RESULT_SPEECH);
Toast.makeText(getApplicationContext(),
"started acitvity for result", //test toast
Toast.LENGTH_SHORT).show();
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_to_text_not_supported),
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(isName)
currentName = text.get(0);
else
currentDes = text.get(0);
dialog.DialogNew(currentName, currentDes);
}
break;
}
}
}
Here is the problem: Normally a dialog appears for the input of speech. But somehow this dialog won't appear. I've tested it, and it shows the 'test toast' (see above), but there is no error and no input dialog. But Why?
Edit: I finally could test it on another device and (finally) I got an error: the google dialog was closed. From the protocol, a null pointer exception, so I guess there must be something wrong with my intent.
So I finally could fix the problem:
In my manifast:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/AppTheme"
android:uiOptions="none" >
android:launchMode="singleInstance"> <!--THIS WON'T WORK-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Somehow you can't use singleInstance as the launch mode of the activity. For my purpose I used singleTask as an alternative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With