You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求基于Android Actions/Assistant的移动端Android Java应用示例

适配Android原生的Google Assistant控制方案(无需Dialogflow/Firebase)

嘿,我太懂你这种想绕开第三方服务、直接用Android原生能力对接Google Assistant的需求了——毕竟折腾Node.js+Firebase总感觉有点“脱裤子放屁”,直接在移动端搞定才是最适配的路子!下面给你两个最贴合Android Java应用的方案,附具体代码示例:

方案一:App Actions(官方推荐,轻量原生)

这是Google专门为Android应用打造的原生对接方案,不需要额外的云端服务,直接通过App内的Intent和XML配置就能实现语音控制。

步骤1:配置语音指令映射(actions.xml)

在你的项目res/xml目录下创建actions.xml文件,定义触发Assistant的语音指令,以及对应的Android Intent:

<?xml version="1.0" encoding="utf-8"?>
<actions:actions xmlns:actions="http://schemas.android.com/apk/res-auto">
    <!-- 定义修改背景色的指令 -->
    <actions:action
        intentName="com.yourpackage.action.CHANGE_BACKGROUND_COLOR"
        queryPatterns="@array/change_background_color_queries">
        <!-- 定义颜色参数,支持自然语言解析 -->
        <actions:parameter
            name="color"
            type="https://schema.org/Color"/>
    </actions:action>
</actions:actions>

然后在res/values/arrays.xml里添加支持的语音指令模板:

<string-array name="change_background_color_queries">
    <item>将背景色改为$color</item>
    <item>把背景换成$color</item>
    <item>设置背景色为$color</item>
</string-array>

步骤2:在Manifest里注册Intent处理组件

假设你用Activity来处理这个指令,就在AndroidManifest.xml里给目标Activity添加Intent过滤器:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="com.yourpackage.action.CHANGE_BACKGROUND_COLOR"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

步骤3:在Java代码里处理Intent逻辑

在你的MainActivity里获取Assistant传递的颜色参数,然后修改背景色:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleChangeBackgroundColorIntent(intent);
}

private void handleChangeBackgroundColorIntent(Intent intent) {
    if ("com.yourpackage.action.CHANGE_BACKGROUND_COLOR".equals(intent.getAction())) {
        // 获取解析后的颜色参数
        String colorStr = intent.getStringExtra("color");
        // 把颜色字符串转成Color对象(这里简单处理,实际可以优化解析逻辑)
        int color;
        switch (colorStr.toLowerCase()) {
            case "黄色":
                color = Color.YELLOW;
                break;
            case "红色":
                color = Color.RED;
                break;
            case "蓝色":
                color = Color.BLUE;
                break;
            default:
                color = Color.WHITE;
                break;
        }
        // 修改背景色
        getWindow().getDecorView().setBackgroundColor(color);
    }
}

测试方法

  • 用Android Studio的「App Actions Test Tool」直接输入语音指令测试
  • 或者在设备上唤醒Google Assistant,说“打开你的应用名,将背景色改为黄色”(第一次可能需要授权)

方案二:Direct Actions(进阶,无需打开App执行)

如果想要更极致的体验——比如不用打开App就能直接执行改背景色的操作,可以用Direct Actions。这个需要在Google Cloud Console做简单配置,但核心逻辑还是在Android端:

  1. 在Google Cloud Console创建项目,启用「Actions on Google」API
  2. 配置Direct Action的触发指令和参数,关联你的Android应用包名
  3. 在Android端用BroadcastReceiver处理Intent,即使App在后台也能响应

代码示例(BroadcastReceiver):

public class BackgroundColorReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("com.yourpackage.action.CHANGE_BACKGROUND_COLOR".equals(intent.getAction())) {
            String colorStr = intent.getStringExtra("color");
            // 这里可以通过SharedPreferences保存颜色值,然后通知MainActivity更新UI
            SharedPreferences prefs = context.getSharedPreferences("AppSettings", Context.MODE_PRIVATE);
            prefs.edit().putString("background_color", colorStr).apply();
            
            // 发送广播通知Activity更新UI
            Intent updateIntent = new Intent("com.yourpackage.action.UPDATE_BACKGROUND");
            context.sendBroadcast(updateIntent);
        }
    }
}

然后在Manifest里注册Receiver:

<receiver android:name=".BackgroundColorReceiver">
    <intent-filter>
        <action android:name="com.yourpackage.action.CHANGE_BACKGROUND_COLOR"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

注意事项

  • 语音指令的queryPatterns要尽量覆盖用户可能的表达方式,提升识别准确率
  • 颜色参数的解析可以用更智能的方式(比如解析十六进制颜色值)
  • 发布前需要在Google Play Console提交App Actions的审核

内容的提问来源于stack exchange,提问作者miro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:20:36