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

.NET MAUI集成Google Assistant遇阻:无法通过语音指令唤起应用

解决.NET MAUI应用与Google Assistant语音交互的配置问题

一、先解决基础指令“Hello Google, open Vehicle Info”唤起应用的问题

1. 检查AndroidManifest.xml的Activity配置

确保MainActivity的包含完整启动配置,同时添加让Assistant识别的必要标签:

<activity android:name=".MainActivity"
          android:exported="true">
    <!-- 默认启动Intent -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <!-- 让Assistant可以通过语音唤起应用 -->
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
</activity>

注意android:exported="true"必须设置,否则Android 12+会阻止外部Intent唤起应用。

2. 验证快捷方式配置(可选,提升Assistant识别率)

如果用快捷方式辅助唤起,确保以下配置正确:

  • Resources/values/strings.xml:
<string name="shortcut_vehicle_info">Vehicle Info</string>
<string name="shortcut_vehicle_info_desc">Open Vehicle Info app to check vehicle details</string>
  • Resources/xml/shortcuts.xml:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="open_vehicle_info"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/shortcut_vehicle_info"
        android:shortcutLongLabel="@string/shortcut_vehicle_info"
        android:shortcutDisabledMessage="@string/shortcut_vehicle_info_desc">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.yourcompany.vehicleinfo"
            android:targetClass="com.yourcompany.vehicleinfo.MainActivity" />
        <!-- 必须添加categories,让Assistant识别 -->
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>
  • 在AndroidManifest.xml的标签中引用快捷方式:
<application ...>
    <meta-data
        android:name="android.app.shortcuts"
        android:resource="@xml/shortcuts" />
</application>

3. 关键验证步骤

  • 用内部测试渠道的测试账号安装应用,等待Google Assistant同步应用信息(可重启Assistant或等待1-2小时)。
  • 在设备上手动打开应用至少一次,确保应用被系统注册。

二、实现进阶指令“Hello Google, give me info about {registration number}”的免提交交互

这需要使用Google的App Actions功能,配置步骤如下:

1. 创建actions.xml配置文件

在Resources/xml下新建actions.xml,定义语音指令的参数和关联的Intent:

<actions:actions xmlns:actions="http://schemas.android.com/apk/res-auto">
    <actions:action
        android:name="actions.intent.GET_THING"
        actions:queryPatterns="@array/vehicle_info_queries">
        <actions:fulfillment
            actions:action="android.intent.action.VIEW"
            actions:targetPackage="com.yourcompany.vehicleinfo"
            actions:targetClass="com.yourcompany.vehicleinfo.MainActivity">
            <actions:parameter-mapping
                actions:intentParameter="thing.name"
                actions:parameter="registrationNumber" />
        </actions:fulfillment>
    </actions:action>
</actions:actions>

2. 添加查询模式字符串数组

在Resources/values/strings.xml中添加:

<string-array name="vehicle_info_queries">
    <item>give me info about $RegistrationNumber</item>
    <item>get vehicle info for $RegistrationNumber</item>
    <item>check details for vehicle $RegistrationNumber</item>
</string-array>

3. 在AndroidManifest.xml中引用actions.xml

在标签内添加:

<meta-data
    android:name="com.google.android.actions"
    android:resource="@xml/actions" />

4. 在MainActivity中处理带参数的Intent

修改MainActivity.cs,获取Intent中的registrationNumber参数:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    HandleIntent(Intent);
}

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    HandleIntent(intent);
}

private void HandleIntent(Intent intent)
{
    if (intent?.Action == Intent.ActionView)
    {
        // 获取车牌参数
        var registrationNumber = intent.GetStringExtra("registrationNumber");
        if (!string.IsNullOrEmpty(registrationNumber))
        {
            // 调用后端获取数据并朗读的逻辑
            FetchVehicleDataAndSpeak(registrationNumber);
        }
    }
}

private void FetchVehicleDataAndSpeak(string regNumber)
{
    // 这里实现你的后端请求和文本朗读逻辑
    // 例如使用TextToSpeech API
}

5. 在Google Play Console配置App Actions测试

  • 打开应用内部测试页面,找到App Actions板块。
  • 上传actions.xml并关联测试账号,确保Assistant能识别自定义指令。

注意事项

  • 所有配置中的包名(com.yourcompany.vehicleinfo)必须和应用包名完全一致。
  • 测试时必须使用已加入内部测试的Google账号,且设备已登录该账号。
  • 若Assistant仍不识别,可尝试清除Assistant缓存后重新测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 16:40:17