Example usage for android.speech RecognizerIntent ACTION_RECOGNIZE_SPEECH

List of usage examples for android.speech RecognizerIntent ACTION_RECOGNIZE_SPEECH

Introduction

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

Prototype

String ACTION_RECOGNIZE_SPEECH

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

Click Source Link

Document

Starts an activity that will prompt the user for speech and send it through a speech recognizer.

Usage

From source file:com.tandong.sa.sherlock.widget.SearchView.java

public SearchView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.c = context;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
        throw new IllegalStateException("SearchView is API 8+ only.");
    }//from  ww  w .ja  va  2 s.c  om

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(getResources().getIdentifier("abs__search_view", "layout", context.getPackageName()), this,
            true);
    // inflater.inflate(R.layout.abs__search_view, this, true);

    mSearchButton = findViewById(
            getResources().getIdentifier("abs__search_button", "id", context.getPackageName()));
    // mSearchButton = findViewById(R.id.abs__search_button);
    mQueryTextView = (SearchAutoComplete) findViewById(
            getResources().getIdentifier("abs__search_src_text", "id", context.getPackageName()));
    // mQueryTextView = (SearchAutoComplete)
    // findViewById(R.id.abs__search_src_text);
    mQueryTextView.setSearchView(this);

    mSearchEditFrame = findViewById(
            getResources().getIdentifier("abs__search_edit_frame", "id", context.getPackageName()));
    // mSearchEditFrame = findViewById(R.id.abs__search_edit_frame);
    mSearchPlate = findViewById(
            getResources().getIdentifier("abs__search_plate", "id", context.getPackageName()));
    // mSearchPlate = findViewById(R.id.abs__search_plate);
    mSubmitArea = findViewById(
            getResources().getIdentifier("abs__submit_area", "id", context.getPackageName()));
    // mSubmitArea = findViewById(R.id.abs__submit_area);
    mSubmitButton = findViewById(
            getResources().getIdentifier("abs__search_go_btn", "id", context.getPackageName()));
    // mSubmitButton = findViewById(R.id.abs__search_go_btn);
    mCloseButton = (ImageView) findViewById(
            getResources().getIdentifier("abs__search_close_btn", "id", context.getPackageName()));
    // mCloseButton = (ImageView) findViewById(R.id.abs__search_close_btn);
    mVoiceButton = findViewById(
            getResources().getIdentifier("abs__search_voice_btn", "id", context.getPackageName()));
    // mVoiceButton = findViewById(R.id.abs__search_voice_btn);
    mSearchHintIcon = (ImageView) findViewById(
            getResources().getIdentifier("abs__search_mag_icon", "id", context.getPackageName()));
    // mSearchHintIcon = (ImageView)
    // findViewById(R.id.abs__search_mag_icon);

    mSearchButton.setOnClickListener(mOnClickListener);
    mCloseButton.setOnClickListener(mOnClickListener);
    mSubmitButton.setOnClickListener(mOnClickListener);
    mVoiceButton.setOnClickListener(mOnClickListener);
    mQueryTextView.setOnClickListener(mOnClickListener);

    mQueryTextView.addTextChangedListener(mTextWatcher);
    mQueryTextView.setOnEditorActionListener(mOnEditorActionListener);
    mQueryTextView.setOnItemClickListener(mOnItemClickListener);
    mQueryTextView.setOnItemSelectedListener(mOnItemSelectedListener);
    mQueryTextView.setOnKeyListener(mTextKeyListener);
    // Inform any listener of focus changes
    mQueryTextView.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            if (mOnQueryTextFocusChangeListener != null) {
                mOnQueryTextFocusChangeListener.onFocusChange(SearchView.this, hasFocus);
            }
        }
    });

    TypedArray a = context.obtainStyledAttributes(attrs,
            new int[] {
                    getResources().getIdentifier("SherlockSearchView", "styleable", context.getPackageName()) },
            0, 0);
    // R.styleable.SherlockSearchView, 0, 0);
    setIconifiedByDefault(a.getBoolean(getResources().getIdentifier("SherlockSearchView_iconifiedByDefault",
            "styleable", context.getPackageName()), true));
    // R.styleable.SherlockSearchView_iconifiedByDefault, true));
    int maxWidth = a.getDimensionPixelSize(getResources().getIdentifier("SherlockSearchView_android_maxWidth",
            "styleable", context.getPackageName()), -1);
    // R.styleable.SherlockSearchView_android_maxWidth, -1);
    if (maxWidth != -1) {
        setMaxWidth(maxWidth);
    }
    CharSequence queryHint = a.getText(getResources().getIdentifier("SherlockSearchView_queryHint", "styleable",
            context.getPackageName()));
    // .getText(R.styleable.SherlockSearchView_queryHint);
    if (!TextUtils.isEmpty(queryHint)) {
        setQueryHint(queryHint);
    }
    int imeOptions = a.getInt(getResources().getIdentifier("SherlockSearchView_android_imeOptions", "styleable",
            context.getPackageName()), -1);
    // R.styleable.SherlockSearchView_android_imeOptions, -1);
    if (imeOptions != -1) {
        setImeOptions(imeOptions);
    }
    int inputType = a.getInt(getResources().getIdentifier("SherlockSearchView_android_inputType", "styleable",
            context.getPackageName()), -1);
    // R.styleable.SherlockSearchView_android_inputType, -1);
    if (inputType != -1) {
        setInputType(inputType);
    }

    a.recycle();

    boolean focusable = true;

    a = context.obtainStyledAttributes(attrs,
            new int[] { getResources().getIdentifier("SherlockView", "styleable", context.getPackageName()) },
            0,
            // a = context.obtainStyledAttributes(attrs,
            // R.styleable.SherlockView, 0,
            0);
    focusable = a.getBoolean(
            getResources().getIdentifier("SherlockView_android_focusable", "styleable",
                    context.getPackageName()),
            // focusable =
            // a.getBoolean(R.styleable.SherlockView_android_focusable,
            focusable);
    a.recycle();
    setFocusable(focusable);

    // Save voice intent for later queries/launching
    mVoiceWebSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
    mVoiceWebSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mVoiceWebSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

    mVoiceAppSearchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mVoiceAppSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    mDropDownAnchor = findViewById(mQueryTextView.getDropDownAnchor());
    if (mDropDownAnchor != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mDropDownAnchor.addOnLayoutChangeListener(new OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
                        int oldTop, int oldRight, int oldBottom) {
                    adjustDropDownSizeAndPosition();
                }
            });
        } else {
            mDropDownAnchor.getViewTreeObserver()
                    .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            adjustDropDownSizeAndPosition();
                        }
                    });
        }
    }

    updateViewsVisibility(mIconifiedByDefault);
    updateQueryHint();
}

From source file:me.willowcheng.makerthings.ui.OpenHABMainActivity.java

private void launchVoiceRecognition() {
    Intent callbackIntent = new Intent(this, OpenHABVoiceService.class);
    callbackIntent.putExtra(OpenHABVoiceService.OPENHAB_BASE_URL_EXTRA, openHABBaseUrl);
    PendingIntent openhabPendingIntent = PendingIntent.getService(this, 0, callbackIntent, 0);

    Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    // Display an hint to the user about what he should say.
    speechIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.info_voice_input));
    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
    speechIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, openhabPendingIntent);

    startActivity(speechIntent);//from w  ww. ja va  2s .  c om
}

From source file:org.openhab.habdroid.ui.OpenHABMainActivity.java

public void checkVoiceRecognition() {
    // Check if voice recognition is present
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm
            .queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() != 0) {
        mVoiceRecognitionEnabled = true;
    }// ww  w .  ja va2 s  . c o  m
}

From source file:me.willowcheng.makerthings.ui.OpenHABMainActivity.java

public void checkVoiceRecognition() {
    // Check if voice recognition is present
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm
            .queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() == 0) {
        //            speakButton.setEnabled(false);
        //            speakButton.setText("Voice recognizer not present");
        Toast.makeText(this, "Voice recognizer not present, voice recognition disabled", Toast.LENGTH_SHORT)
                .show();/*from   ww  w.j  a va  2 s .  com*/
    } else {
        mVoiceRecognitionEnabled = true;
    }
}

From source file:com.androzic.vnspeech.MapFragment.java

private void onTalk() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    Locale locale = new Locale("vi", "VN");
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, locale);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "??c lnh");
    try {/* www.  j  ava2s  .c  om*/
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getContext(), "Thit b ny khng h tr nhn dng gi?ng ni.",
                Toast.LENGTH_SHORT).show();
    }
}

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

/**
 * Finds all the views we need and configure them properly.
 */// w  ww . ja v  a 2s . c  o m
private void setupViews() {
    mLauncherView = findViewById(R.id.launcher);
    mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
    mFocusHandler = mDragLayer.getFocusIndicatorHelper();
    mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
    mQsbContainer = mDragLayer.findViewById(
            mDeviceProfile.isVerticalBarLayout() ? R.id.workspace_blocked_row : R.id.qsb_container);
    mWorkspace.initParentViews(mDragLayer);

    mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

    // Setup the drag layer
    mDragLayer.setup(this, mDragController, mAllAppsController);

    // Setup the hotseat
    mHotseat = (Hotseat) findViewById(R.id.hotseat);
    if (mHotseat != null) {
        mHotseat.setOnLongClickListener(this);
    }

    // Setup the overview panel
    setupOverviewPanel();

    setuphome();

    // Setup the workspace
    mWorkspace.setHapticFeedbackEnabled(false);
    mWorkspace.setOnLongClickListener(this);
    mWorkspace.setup(mDragController);
    // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
    // default state, otherwise we will update to the wrong offsets in RTL
    mWorkspace.lockWallpaperToDefaultPage();
    mDragController.addDragListener(mWorkspace);

    // Get the search/delete/uninstall bar
    mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);

    // Setup Apps and Widgets
    mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
    mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
    if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
        mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
    } else {
        mAppsView.setSearchBarController(new DefaultAppSearchController());
    }

    // Setup the drag controller (drop targets have to be added in reverse order in priority)
    mDragController.setDragScoller(mWorkspace);
    mDragController.setScrollView(mDragLayer);
    mDragController.setMoveTarget(mWorkspace);
    mDragController.addDropTarget(mWorkspace);
    mDropTargetBar.setup(mDragController);

    if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
        mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
    }

    if (TestingUtils.MEMORY_DUMP_ENABLED) {
        TestingUtils.addWeightWatcher(this);
    }

    FrameLayout gBar = (FrameLayout) findViewById(R.id.g_bar);

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        FrameLayout.LayoutParams gBarLayout = (FrameLayout.LayoutParams) gBar.getLayoutParams();
        gBarLayout.width = Utils.getScreenXDimension(this) - Utils.getScreenXDimension(this) / 6;
        gBar.setLayoutParams(gBarLayout);
    } else {
        FrameLayout.LayoutParams gBarLayout = (FrameLayout.LayoutParams) gBar.getLayoutParams();
        gBarLayout.width = Utils.getScreenYDimension(this) - Utils.getScreenYDimension(this) / 12;
        gBar.setLayoutParams(gBarLayout);
    }

    gBar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            startSearch("", false, null, true);
        }
    });

    ImageView gSearch = (ImageView) findViewById(R.id.g_search);
    gSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            startSearch("", false, null, true);
        }
    });

    FrameLayout.LayoutParams gSearchLayout = (FrameLayout.LayoutParams) gSearch.getLayoutParams();
    gSearchLayout.leftMargin = 30;
    gSearch.setLayoutParams(gSearchLayout);

    ImageView gSearchMic = (ImageView) findViewById(R.id.g_search_mic);
    if (IS_ALLOW_MIC) {
        gSearchMic.setVisibility(View.VISIBLE);
        gSearchMic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                startActivityForResult(intent, RECOGNIZER_REQ_CODE);
            }
        });
    } else {
        gSearchMic.setVisibility(View.GONE);
    }

    FrameLayout.LayoutParams gSearchMicLayout = (FrameLayout.LayoutParams) gSearchMic.getLayoutParams();
    gSearchMicLayout.rightMargin = 30;
    gSearchMic.setLayoutParams(gSearchMicLayout);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (Utilities.isAllowNightModePrefEnabled(getApplicationContext())) {
            gBar.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.shape_night));
            gSearch.setImageDrawable(
                    ContextCompat.getDrawable(getApplicationContext(), R.drawable.g_icon_night));
            gSearch.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.night_color));
            gSearchMic
                    .setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.mic_night));
            gSearchMic.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.night_color));
        } else {
            gBar.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.shape));
            gSearch.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.g_icon));
            gSearch.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.white));
            gSearchMic.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.mic));
            gSearchMic
                    .setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.white));
        }
    }

    if (!Utilities.isAllowPersisentSearchBarPrefEnabled(getApplicationContext())) {
        gBar.setVisibility(View.GONE);
        gSearch.setVisibility(View.GONE);
        gSearchMic.setVisibility(View.GONE);
    } else {
        gBar.setVisibility(View.VISIBLE);
        gSearch.setVisibility(View.VISIBLE);
        if (IS_ALLOW_MIC) {
            gSearchMic.setVisibility(View.VISIBLE);
        } else {
            gSearchMic.setVisibility(View.GONE);
        }
    }

}

From source file:org.botlibre.sdk.activity.ChatActivity.java

@TargetApi(23)
private void beginListening() {
    lastReply = System.currentTimeMillis();
    setStreamVolume();/*  ww w . j  a  va 2 s  .c o  m*/
    debug("beginListening:");

    try {
        if (!MainActivity.handsFreeSpeech) {
            return;
        }
        if (MainActivity.handsFreeSpeech) {
            muteMicBeep(true);
            isListening = true;
        }

        if (!MainActivity.listenInBackground) {
            muteMicBeep(false);
            return;
        }

    } catch (Exception ignore) {
        Log.e("Error", "BeginListening");
    }

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    if (MainActivity.offlineSpeech) {
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);

        if (!this.failedOfflineLanguage) {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
            // intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
        }
        intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
    } else {
        if (MainActivity.voice.language != null && !MainActivity.voice.language.isEmpty()) {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);
            if (!this.failedOfflineLanguage) {
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, MainActivity.voice.language);
            }
        } else {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en");
            if (!this.failedOfflineLanguage) {
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
            }
        }
    }

    try {
        Log.d("BeginListening", "StartListening");
        this.speech.startListening(intent);
        setMicIcon(true, false);
    } catch (ActivityNotFoundException a) {
        Log.d("BeginListening", "CatchError: " + a.getMessage());
        Toast t = Toast.makeText(getApplicationContext(), "Your device doesn't support Speech to Text",
                Toast.LENGTH_SHORT);
        t.show();
    }
}

From source file:com.rfo.basic.Run.java

public static Intent buildVoiceRecognitionIntent() {
    if (sttPrompt == null) {
        sttPrompt = sttDefaultPrompt;/*w w  w. j a  v  a  2  s  . c  o m*/
    }
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, sttPrompt);
    return intent;
}