Google语音操作:当Activity.isVoiceInteraction为true时?

Jos*_*seF 5 android google-voice-search google-voice-actions voice-interaction

内容

我正在尝试将Google语音操作集成我的应用中。我已经看过并理解了(或至少是我的想法)google codelabs-io2015示例,在此示例中,如果您不修改代码,一切都会按预期工作。当您尝试使此示例适应实际用例时,问题就开始了。

问题

所以,我的问题是我正在尝试实施搜索语音操作,但是Activity#isVoiceInteraction始终为false。我最终不了解该活动何时以及为何(以及何时未链接)到语音交互器。

研究

查看ActivityActivity#isVoiceInteractionActivity#getVoiceInteractor API级别23的源代码,发现以下内容:

 /**
 * Check whether this activity is running as part of a voice interaction with the user.
 * If true, it should perform its interaction with the user through the
 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
 */
public boolean isVoiceInteraction() {
    return mVoiceInteractor != null;
}
Run Code Online (Sandbox Code Playgroud)

/**
 * Retrieve the active {@link VoiceInteractor} that the user is going through to
 * interact with this activity.
 */
public VoiceInteractor getVoiceInteractor() {
    return mVoiceInteractor;
}
Run Code Online (Sandbox Code Playgroud)

并且mVoiceInteractor仅在attach函数上初始化,如下所示:

final void attach(Context context, ActivityThread aThread,
        Instrumentation instr, IBinder token, int ident,
        Application application, Intent intent, ActivityInfo info,
        CharSequence title, Activity parent, String id,
        NonConfigurationInstances lastNonConfigurationInstances,
        Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
    ...
    mLastNonConfigurationInstances = lastNonConfigurationInstances;
    if (voiceInteractor != null) {
        if (lastNonConfigurationInstances != null) {
            mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
        } else {
            mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
                    Looper.myLooper());
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)