如何在Android 4.1中使用ACTION_VOICE_SEARCH_HANDS_FREE?

时间:2012-09-18 10:54:32

标签: android speech-recognition android-4.2-jelly-bean

我正在尝试在 Android 4.1 中使用ACTION_VOICE_SEARCH_HANDS_FREE

我用这种方式:

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

它适用于ACTION_RECOGNIZE_SPEECH,但ACTION_VOICE_SEARCH_HANDS_FREE我有这个:

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

我如何使用ACTION_VOICE_SEARCH_HANDS_FREE

2 个答案:

答案 0 :(得分:1)

@Kaarel回答正确,但要提供更多信息:

活动需要在清单中注册以下内容:

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

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

Google搜索已针对此意图进行了注册,但只有在屏幕开启且未锁定时才会对其进行响应:请参阅以下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();
            }
        }

此功能目前非常冗余due to this linked bug,其中系统应用程序覆盖优先级,从而阻止用户选择已安装的替代方案。

当你创建了这样一个应用程序并且用户无法使用它时,真的很烦人.............确实。

答案 1 :(得分:0)

您需要在设备上安装包含响应ACTION_VOICE_SEARCH_HANDS_FREE意图的活动的应用。所以问题就变成了:哪些应用支持这种意图?我所知道的唯一答案是琐碎的,“自己实现这样的应用程序”,所以我也对这个问题感兴趣,但我不确定它是否适合Stackoverflow格式。