5

I am working on an android application in which i have implemented voice recognition and TTS. So i was thinking to launch settings screen for both google voice recognition and TTS to allow user to change settings from within the application. I have implemented TTS settings successfully by using following code:

intent = new Intent();
intent.setAction("com.android.settings.TTS_SETTINGS");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);

Now I want to show system's "google voice recognition settings" in my application to allow user to change language options etc. I have searched a lot... Done a lot of hit and try but failed to load voice recognition settings screen. Please tell me how i can implement that. Thanks in advance...

doppelgreener
  • 4,809
  • 10
  • 46
  • 63
Pargat
  • 769
  • 9
  • 23

2 Answers2

11

The @brandall answer doesn't work at Android 5.1 for me such as another component name is used for the voice recognition settings there.

/**
 * 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;
}

EDIT: updated with the latest component name

Eugene Popovich
  • 3,343
  • 2
  • 30
  • 33
  • Worked for Android 4.4.2 – igla Jan 04 '17 at 19:28
  • @AdrianBuciuman looks like that settings are no more available in the system settings – Eugene Popovich Nov 22 '17 at 06:32
  • 1
    @httpdispatch on Android 7.1.1 `Settings -> Languages & Input -> Virtual keyboard -> Google Voice typing` the settings are displayed. – Adrian Buciuman Nov 22 '17 at 10:16
  • @AdrianBuciuman wow, nice found. I will test that new window and update the answer – Eugene Popovich Nov 22 '17 at 10:20
  • 1
    @AdrianBuciuman i've added new component name to the answer but third party app can't open that window because of SecurityException. I am not sure whether there are any workarounds for that – Eugene Popovich Nov 22 '17 at 10:27
  • 1
    @AdrianBuciuman looks that i've found proper component name. Please check the updated answer. Thank you very much for the found. – Eugene Popovich Nov 22 '17 at 10:34
  • @httpdispatch How did you find this class? because if i do `adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'" ` it displays this class `com.google.android.googlequicksearchbox/com.google.android.apps.gsa.velvet.ui.settings.SettingsActivity` – Adrian Buciuman Nov 22 '17 at 10:50
  • 1
    @AdrianBuciuman i've opened the window you've found at "Virtual Keyboard" settings and checked logcat output. There i found next record /ActivityManager: START u0 {act=android.intent.action.MAIN cmp=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.settingsui.VoiceSearchPreferences} from uid 1000 on display 0 – Eugene Popovich Nov 22 '17 at 10:53
5

I was stuck on this for ages too...

    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);
    }

Hope it does for you too...

EDIT: As pointed out in the comments, this changed in the Jelly Bean version of the Google Search App. To catch any potential update issues where you can't use Build.Version, you can use something along these lines:

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();
}
}
brandall
  • 6,094
  • 4
  • 49
  • 103
  • Thank man... You saved my life. It works perfectly... :) Can you please tell me how did you came to know which ComponentName/path will work for VoiceSearchPreferences. Coz i have also spent a lot of effort on to work it out but couldn't succeed. Thanks again..!!! – Pargat Mar 17 '12 at 20:20
  • @pargat It was a LOT of trial and error... I have a useful application on my device called 'Package Explorer' which shows details of the activities and components available to use. It's good to have to hand and helped me solve how to display these settings. [link](https://play.google.com/store/apps/details?id=org.andr.pkgexp) – brandall Mar 18 '12 at 21:03
  • This doesn't work after Gingerbread. http://stackoverflow.com/questions/11860229/how-to-display-voice-recognition-settings-screen-programmatically/11860230#11860230 – Mark Aug 16 '12 at 05:39
  • I didn't know about the Jelly Bean change, thank you. But the above should work fine on all Honeycomb and ICS devices? I've have lots of users/testers confirm... +works on all of my test devices – brandall Aug 16 '12 at 12:41
  • 1
    This will work for all devices before Jelly bean after JellyBean http://stackoverflow.com/questions/11860229/how-to-display-voice-recognition-settings-screen-programmatically/11860230#11860230 – Chaitu Aug 26 '13 at 05:59
  • @Chaitu - Updated Answer - You cannot check this by Build.Version, as it is installed application dependent, not OS version. – brandall Aug 26 '13 at 17:28