Example usage for android.speech RecognizerIntent ACTION_WEB_SEARCH

List of usage examples for android.speech RecognizerIntent ACTION_WEB_SEARCH

Introduction

In this page you can find the example usage for android.speech RecognizerIntent ACTION_WEB_SEARCH.

Prototype

String ACTION_WEB_SEARCH

To view the source code for android.speech RecognizerIntent ACTION_WEB_SEARCH.

Click Source Link

Document

Starts an activity that will prompt the user for speech, send it through a speech recognizer, and either display a web search result or trigger another type of action based on the user's speech.

Usage

From source file:com.android.soma.Launcher.java

public void startVoice() {
    try {/*  w w  w. j  a v  a2 s  . c  o m*/
        final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        ComponentName activityName = searchManager.getGlobalSearchActivity();
        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (activityName != null) {
            intent.setPackage(activityName.getPackageName());
        }
        startActivity(null, intent, "onClickVoiceButton");
    } catch (ActivityNotFoundException e) {
        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivitySafely(null, intent, "onClickVoiceButton");
    }
}

From source file:com.android.launcher2.Launcher.java

private boolean updateVoiceSearchIcon(boolean searchVisible) {
    final View voiceButtonContainer = findViewById(R.id.voice_button_container);
    final View voiceButton = findViewById(R.id.voice_button);
    final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);

    // We only show/update the voice search icon if the search icon is enabled as well
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();

    ComponentName activityName = null;//  www .  j  a v a 2  s  .  c  om
    if (globalSearchActivity != null) {
        // Check if the global search activity handles voice search
        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
        intent.setPackage(globalSearchActivity.getPackageName());
        activityName = intent.resolveActivity(getPackageManager());
    }

    if (activityName == null) {
        // Fallback: check if an activity other than the global search activity
        // resolves this
        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
        activityName = intent.resolveActivity(getPackageManager());
    }
    if (searchVisible && activityName != null) {
        int coi = getCurrentOrientationIndexForGlobalIcons();
        sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(R.id.voice_button, activityName,
                R.drawable.ic_home_voice_search_holo, TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
        if (sVoiceSearchIcon[coi] == null) {
            sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(R.id.voice_button, activityName,
                    R.drawable.ic_home_voice_search_holo, TOOLBAR_ICON_METADATA_NAME);
        }
        if (voiceButtonContainer != null)
            voiceButtonContainer.setVisibility(View.VISIBLE);
        voiceButton.setVisibility(View.VISIBLE);
        if (voiceButtonProxy != null) {
            voiceButtonProxy.setVisibility(View.VISIBLE);
        }
        invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
        return true;
    } else {
        if (voiceButtonContainer != null)
            voiceButtonContainer.setVisibility(View.GONE);
        voiceButton.setVisibility(View.GONE);
        if (voiceButtonProxy != null) {
            voiceButtonProxy.setVisibility(View.GONE);
        }
        return false;
    }
}

From source file:com.android.soma.Launcher.java

protected boolean updateVoiceSearchIcon(boolean searchVisible) {
    final View voiceButtonContainer = findViewById(R.id.voice_button_container);
    final View voiceButton = findViewById(R.id.voice_button);

    // We only show/update the voice search icon if the search icon is enabled as well
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();

    ComponentName activityName = null;/*from w  w  w . j  a  v  a 2s  . c om*/
    if (globalSearchActivity != null) {
        // Check if the global search activity handles voice search
        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
        intent.setPackage(globalSearchActivity.getPackageName());
        activityName = intent.resolveActivity(getPackageManager());
    }

    if (activityName == null) {
        // Fallback: check if an activity other than the global search activity
        // resolves this
        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
        activityName = intent.resolveActivity(getPackageManager());
    }
    if (searchVisible && activityName != null) {
        int coi = getCurrentOrientationIndexForGlobalIcons();
        sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(R.id.voice_button, activityName,
                R.drawable.ic_home_voice_search_holo, TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
        if (sVoiceSearchIcon[coi] == null) {
            sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(R.id.voice_button, activityName,
                    R.drawable.ic_home_voice_search_holo, TOOLBAR_ICON_METADATA_NAME);
        }
        if (voiceButtonContainer != null)
            voiceButtonContainer.setVisibility(View.VISIBLE);
        voiceButton.setVisibility(View.VISIBLE);
        updateVoiceButtonProxyVisible(false);
        invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
        return true;
    } else {
        if (voiceButtonContainer != null)
            voiceButtonContainer.setVisibility(View.GONE);
        if (voiceButton != null)
            voiceButton.setVisibility(View.GONE);
        updateVoiceButtonProxyVisible(false);
        return false;
    }
}