如何通过自定义Intent与语音命令实现Hey Google打开Android应用
解决Google语音命令直接打开自定义App的问题
核心配置步骤
1. 完善启动Activity的Intent Filter
确保主Activity同时包含默认启动入口和可被Google Assistant触发的Deep Link配置,避免浏览器拦截:
<activity android:name=".MainActivity" android:exported="true"> <!-- 默认桌面启动入口 --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- Google Assistant触发专用的Intent Filter --> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 替换为你的应用专属scheme和host --> <data android:scheme="exampleapp" android:host="launch" /> </intent-filter> </activity>
android:autoVerify="true" 用于让Google验证Deep Link归属,防止被其他应用或浏览器劫持。
2. 配置App Actions的语音命令映射
在res/xml/actions.xml中定义语音触发模板和Intent跳转逻辑,提供两种方案:
方案一:使用内置OPEN_APP意图(推荐)
利用Google官方内置意图,兼容性更好:
<?xml version="1.0" encoding="utf-8"?> <actions> <!-- 打开应用的命令配置 --> <action intentName="actions.intent.OPEN_APP"> <fulfillment intentUri="intent://launch#Intent;scheme=exampleapp;package=com.example.exampleapp;end" /> <parameter name="app"> <entity-set-reference entitySetId="my_app_entity" /> </parameter> <!-- 可扩展更多语音指令示例 --> <example phrase="打开ExampleApp" /> <example phrase="帮我启动ExampleApp" /> </action> <!-- 定义应用名称实体,让Assistant识别你的应用 --> <entity-set entitySetId="my_app_entity"> <entity value="ExampleApp" /> <entity value="我的ExampleApp" /> </entity-set> <!-- 保留你已实现的搜索类命令 --> <action intentName="actions.intent.GET_THING"> <fulfillment intentUri="intent://search#Intent;scheme=exampleapp;package=com.example.exampleapp;S.query={query};end" /> <!-- 原有搜索相关配置... --> </action> </actions>
方案二:自定义意图
若需完全自定义触发逻辑,可使用自定义意图名称:
<?xml version="1.0" encoding="utf-8"?> <actions> <action intentName="com.example.exampleapp.LAUNCH_APP"> <fulfillment intentUri="intent://launch#Intent;scheme=exampleapp;package=com.example.exampleapp;end" /> <parameter name="trigger"> <entity-set-reference entitySetId="launch_triggers" /> </parameter> <example phrase="打开ExampleApp" /> <example phrase="启动ExampleApp" /> </action> <entity-set entitySetId="launch_triggers"> <entity value="打开ExampleApp" /> <entity value="启动ExampleApp" /> </entity-set> <!-- 保留原有搜索配置 --> <action intentName="actions.intent.GET_THING"> <!-- ...原有代码 --> </action> </actions>
3. 关联App Actions到应用
在AndroidManifest.xml的<application>标签内添加meta-data,让Google Assistant识别配置:
<application ...> <meta-data android:name="com.google.android.actions" android:resource="@xml/actions" /> <!-- ...其他应用配置 --> </application>
验证与排查
- 本地Intent测试
先通过adb命令验证Intent是否能直接启动应用,排除代码逻辑问题:
adb shell am start -a android.intent.action.VIEW -d "exampleapp://launch"
- 同步Assistant配置
- 清除Google Assistant缓存:设置 → 应用 → Google → 存储 → 清除缓存
- 重启设备后重新测试语音命令
避免冲突
若仍跳转到浏览器,检查是否有其他应用注册了相同的exampleappscheme,可在你的Intent Filter中增加android:priority="100"提升优先级。测试环境要求
- 应用需通过Google Play内部测试/正式渠道发布(或使用Android Studio的App Actions Test Tool进行本地测试)
- 设备登录的Google账号需有权限访问应用测试渠道
内容的提问来源于stack exchange,提问作者Atman Bhatt
相关产品推荐
相关产品推荐

