How can i use ACTION_VOICE_SEARCH_HANDS_FREE in Android 4.1?
Asked Answered
P

2

8

I'm trying to use ACTION_VOICE_SEARCH_HANDS_FREE in Android 4.1.

I use this way:

Intent intent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
intent.putExtra(RecognizerIntent.EXTRA_SECURE, true);
startActivityForResult(intent, RECORD_CODE);

It works fine with ACTION_RECOGNIZE_SPEECH but with ACTION_VOICE_SEARCH_HANDS_FREE I has this:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.action.VOICE_SEARCH_HANDS_FREE (has extras) }

How can i use ACTION_VOICE_SEARCH_HANDS_FREE?

Penn answered 18/9, 2012 at 10:54 Comment(1)
@ Rai220 : Here what is RECORD_CODE ?Twirp
L
2

@Kaarel answered correctly, but to supply a little more information:

An activity needs to register the following in the manifest:

    <intent-filter >
        <action android:name="android.speech.action.VOICE_SEARCH_HANDS_FREE" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

Google Search is registered for this intent, but it will only react to it when the screen is on and not locked: See below taken from AudioService

private void startVoiceBasedInteractions(boolean needWakeLock) {
        Intent voiceIntent = null;
        // select which type of search to launch:
        // - screen on and device unlocked: action is ACTION_WEB_SEARCH
        // - device locked or screen off: action is ACTION_VOICE_SEARCH_HANDS_FREE
        //    with EXTRA_SECURE set to true if the device is securely locked
        PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
        boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
        if (!isLocked && pm.isScreenOn()) {
            voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH);
        } else {
            voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
            voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE,
                    isLocked && mKeyguardManager.isKeyguardSecure());
        }
        // start the search activity
        if (needWakeLock) {
            mMediaEventWakeLock.acquire();
        }
        try {
            if (voiceIntent != null) {
                voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                mContext.startActivity(voiceIntent);
            }
        } catch (ActivityNotFoundException e) {
            Log.w(TAG, "No activity for search: " + e);
        } finally {
            if (needWakeLock) {
                mMediaEventWakeLock.release();
            }
        }

This feature is pretty redundant currently due to this linked bug, where system applications are overriding the priority, thus preventing the user choosing an installed alternative.

Which when you've created such an application and users can't make use of it, is really very annoying............. indeed.

Legality answered 19/8, 2013 at 18:54 Comment(0)
P
0

You need to install on your device an app that contains an activity that responds to the ACTION_VOICE_SEARCH_HANDS_FREE intent. So the question becomes: which apps support this intent? The only answer that I know of is the trivial one, "implement such an app yourself", so I'm also interested in this question, but I'm not sure if it fits the Stackoverflow format.

Papaya answered 20/9, 2012 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.