kfx*_*fx9 5 java android dialog speech android-intent
所有你需要知道的:我有一个带有按钮的对话框.按下按钮时,我想在MainActivity中启动语音识别.(该对话框由另一个类创建,我通过界面处理点击).
所以这是相关的代码:(在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题出在这里:通常会出现一个语音输入对话框.但不知何故,这个对话框不会出现.我测试了它,它显示了'测试吐司'(见上文),但没有错误,也没有输入对话框.但为什么?
编辑:我终于可以在另一台设备上测试它(最后)我收到一个错误:谷歌对话框已关闭.从协议,一个空指针异常,所以我想我的意图肯定有问题.
所以我终于可以解决这个问题了:
在我的清单中:
<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>
Run Code Online (Sandbox Code Playgroud)
不知何故,您不能使用 singleInstance 作为活动的启动模式。出于我的目的,我使用 singleTask 作为替代方案。
| 归档时间: |
|
| 查看次数: |
1098 次 |
| 最近记录: |