我正在开发一个Android应用程序,我已经实现了语音识别和TTS。所以我打算启动谷歌语音识别和TTS的设置屏幕,以允许用户从应用程序内更改设置。 我使用以下代码成功实现了TTS设置:
intent = new Intent();
intent.setAction("com.android.settings.TTS_SETTINGS");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
现在我想在我的应用程序中显示系统的“谷歌语音识别设置”,以允许用户更改语言选项等。我搜索了很多...完成了大量的点击并尝试但未能加载语音识别设置屏幕。请告诉我如何实现这一点。 提前谢谢......
答案 0 :(得分:10)
@brandall答案对我来说不适用于Android 5.1,例如其他组件名称用于语音识别设置。
/**
* Open speech recognition settings activity
*
* @return true in case activity was launched, false otherwise
**/
public boolean openSpeechRecognitionSettings() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
boolean started = false;
ComponentName[] components = new ComponentName[]{
new ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.settingsui.VoiceSearchPreferences"),
new ComponentName("com.google.android.voicesearch", "com.google.android.voicesearch.VoiceSearchPreferences"),
new ComponentName("com.google.android.googlequicksearchbox", "com.google.android.voicesearch.VoiceSearchPreferences"),
new ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.velvet.ui.settings.VoiceSearchPreferences")
};
for (ComponentName componentName : components) {
try {
intent.setComponent(componentName);
startActivity(intent);
started = true;
break;
} catch (final Exception e) {
Timber.e(e, null);
}
}
return started;
}
编辑:使用最新的组件名称
进行更新答案 1 :(得分:5)
我也被困在这个年代......
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(newComponentName("com.google.android.voicesearch","com.google.android.voicesearch.VoiceSearchPreferences"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
希望它也适合你......
编辑:正如评论中所指出的,谷歌搜索应用程序的Jelly Bean版本发生了变化。要捕获任何无法使用Build.Version的潜在更新问题,您可以使用以下内容:
try {
final Intent vsInt = new Intent(Intent.ACTION_MAIN);
vsInt.setComponent(new ComponentName("com.google.android.voicesearch",
"com.google.android.voicesearch.VoiceSearchPreferences"));
vsInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(vsInt);
} catch (final Exception e) {
try {
final Intent vsjInt = new Intent(Intent.ACTION_MAIN);
vsjInt.setComponent(new ComponentName("com.google.android.googlequicksearchbox", "com.google.android.voicesearch.VoiceSearchPreferences"));
vsjInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(vsjInt);
} catch (final Exception e1) {
e1.printStackTrace();
}
}