如何使用 Google Assist API 实施助手

How to implement an Assistant with Google Assist API

我一直在查看和阅读有关 Google Now on Tap(来自 http://developer.android.com/training/articles/assistant.html)的信息。

从那篇文章中很有趣地发现,Now on Tap 是基于 Google 的 Assist API 与 Marshmallow 捆绑在一起的,我们似乎有可能开发自己的助手(本文中使用的术语 Google 是指使用 API.

的应用程序,例如 Now on Tap)

然而,提到的文章只是非常简要地讨论了如何使用 Assist API,即使花了几天时间搜索,我也找不到任何关于如何使用它来开发自定义助手的信息它在互联网上。没有文档,也没有示例。

我想知道你们中是否有人可以分享 Assist API 的经验?任何帮助表示赞赏。

谢谢

从 Android 6.0 开始,您绝对可以使用助手 API 实现个人助理,就像 Google Now on Tap 一样。官方开发者 (http://developer.android.com/training/articles/assistant.html) 指南准确地告诉了你应该如何实现它。

Some developers may wish to implement their own assistant. As shown in Figure 2, the active assistant app can be selected by the Android user. The assistant app must provide an implementation of VoiceInteractionSessionService and VoiceInteractionSession as shown in this example and it requires the BIND_VOICE_INTERACTION permission. It can then receive the text and view hierarchy represented as an instance of the AssistStructure in onHandleAssist(). The assistant receives the screenshot through onHandleScreenshot().

Commonsware 有四个用于基本 Assist API 用法的演示。 TapOffNow (https://github.com/commonsguy/cw-omnibus/tree/master/Assist/TapOffNow) 应该足以让您入门。

您不必使用 onHandleScreenshot() 来获取相关的文本数据,onHandleAssist() 中的 AssistStructure 将为您提供一个根 ViewNode,它通常包含您在屏幕上可以看到的所有内容。

您可能还需要实现某种功能来快速定位您想要关注的特定 ViewNode,使用递归搜索从此根 ViewNode 的子节点。

有一个完整的示例here,但是太复杂了,无法开始。 这是我的示例适用于 android 7.1.1

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eaydin79.voiceinteraction">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme" >
        <service 
            android:name="voiceInteractionService"
            android:permission="android.permission.BIND_VOICE_INTERACTION" >
            <meta-data 
                android:name="android.voice_interaction"
                android:resource="@xml/interaction_service" />
            <intent-filter>
                <action android:name="android.service.voice.VoiceInteractionService" />
            </intent-filter>
        </service>
        <service 
            android:name="voiceInteractionSessionService"
            android:permission="android.permission.BIND_VOICE_INTERACTION" >
        </service>
    </application>
</manifest>

这是存储在 res\xml 文件夹中的 interaction_service.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<voice-interaction-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:sessionService="com.eaydin79.voiceinteraction.voiceInteractionSessionService"
    android:recognitionService="com.eaydin79.voiceinteraction.voiceInteractionService"
    android:supportsAssist="true" />

voiceInteractionService.java

package com.eaydin79.voiceinteraction;
import android.service.voice.VoiceInteractionService;
import android.service.voice.VoiceInteractionSession;

public class voiceInteractionService extends VoiceInteractionService {
    @Override
    public void onReady() {
        super.onReady();
    }
}

voiceInteractionSessionService.java

package com.eaydin79.voiceinteraction;
import android.os.Bundle;
import android.service.voice.VoiceInteractionSession;
import android.service.voice.VoiceInteractionSessionService;

public class voiceInteractionSessionService extends VoiceInteractionSessionService {    
    @Override
    public VoiceInteractionSession onNewSession(Bundle bundle) {
         return new voiceInteractionSession(this);
    }
}

voiceInteractionSession.java

package com.eaydin79.voiceinteraction;
import android.app.VoiceInteractor;
import android.content.Context;
import android.os.Bundle;
import android.service.voice.VoiceInteractionSession;
import android.media.AudioManager;

public class voiceInteractionSession extends VoiceInteractionSession {
   
    voiceInteractionSession(Context context) {
        super(context);
    }

    @Override
    public void onShow(Bundle args, int showFlags) {
        super.onShow(args, showFlags);
        //whatever you want to do when you hold the home button 
        //i am using it to show volume control slider
        AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        if (audioManager != null) audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
        hide();
    }

}