如何通过Google Actions实现应用内深度链接至指定Activity?
实现Google Assistant/Actions深度链接到应用特定Activity的解决方案
没问题,你完全可以通过Google App Actions实现深度链接到应用内的特定Activity,我来一步步帮你梳理解决方案,同时搞定你遇到的Webhook 206错误问题。
一、别只盯着Dialogflow的App Management Agents,核心用App Actions
Dialogflow那个预构建的App Management Agents主要是管应用基础操作的(比如打开、关闭、调偏好),要实现跳应用内部Activity,得结合App Actions——它能直接通过Android的Intent或者App Links触发指定页面,还能和Dialogflow配合处理自然语言请求,完美适配你的需求。
二、先给你的Android应用做基础配置
首先得让你的应用能被外部调用打开特定Activity,两种方式选一个就行:
方式1:配置Android App Links
在AndroidManifest.xml里给目标Activity加对应的<intent-filter>,记得开android:autoVerify="true",还要完成数字资产链接验证(确保Google能关联你的域名和应用)。示例代码:
<activity android:name=".features.settings.ProfileActivity" android:exported="true"> <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示例,比如myapp://profile --> <data android:scheme="myapp" android:host="profile" /> <!-- HTTPS App Links示例,比如https://myapp.example.com/profile --> <data android:scheme="https" android:host="myapp.example.com" android:pathPrefix="/profile" /> </intent-filter> </activity>
方式2:用自定义Intent动作
给目标Activity定义一个专属动作,比如com.myapp.action.OPEN_PROFILE,然后在<intent-filter>里加上这个动作,这样Assistant就能通过触发这个Intent直接打开页面:
<activity android:name=".features.settings.ProfileActivity" android:exported="true"> <intent-filter> <action android:name="com.myapp.action.OPEN_PROFILE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
三、在Actions Console配置App Actions
- 打开Actions Console进入你的项目,找到App Actions模块(没的话先添加)。
- 新建一个App Action,设置触发场景(比如“打开我的应用的个人资料页面”)。
- 配置Invocation:把用户可能说的短语加进去,比如“打开我的应用的个人资料”“去我的应用的设置页”。
- 配置Fulfillment:选“Android App”,挑你的应用包名,然后选触发方式:
- 用App Link的话,直接填对应的链接(比如
myapp://profile)。 - 用自定义Intent的话,选“Custom Intent”,填你定义的动作名称(比如
com.myapp.action.OPEN_PROFILE),需要传参数的话也可以在这里加。
- 用App Link的话,直接填对应的链接(比如
四、如果要结合Dialogflow的话这么搞(可选)
要是你想用Dialogflow处理更复杂的交互,比如用户问“我要改个人资料”,可以这么做:
- 在Dialogflow里建个自定义意图(比如叫
OpenProfileIntent),把用户可能的查询语句加进去。 - 给这个意图开Webhook,然后在你的Webhook服务里返回触发App Action的响应。示例JSON响应结构:
{ "fulfillmentMessages": [ { "platform": "ACTIONS_ON_GOOGLE", "simpleResponses": { "simpleResponses": [ { "textToSpeech": "好的,这就打开你的个人资料页面" } ] } } ], "payload": { "google": { "expectUserResponse": false, "richResponse": { "items": [ { "simpleResponse": { "textToSpeech": "好的,这就打开你的个人资料页面" } } ] }, "systemIntent": { "intent": "actions.intent.OPEN_APP", "parameters": { "appAction": { "action": "com.myapp.action.OPEN_PROFILE" // 用App Link的话就换成"url": "myapp://profile" } }, "data": { "@type": "type.googleapis.com/google.actions.v2.OpenedAppActionSpec" } } } } }
五、搞定Webhook Error (206)
这个错误是“部分内容请求失败”,常见原因和解决方法:
- 响应格式不对:检查你的Webhook返回的JSON有没有语法错误(比如缺逗号、引号不配对),必须严格符合Actions on Google的响应规范(就像上面的示例那样)。
- 响应被截断了:206意味着服务器只返回了部分内容,可能是你的Webhook服务中途挂了、超时了,或者内存不够生成完整响应。检查服务的日志,看看是不是有崩溃或者超时的情况。
- HTTPS证书有问题:如果你的Webhook用HTTPS,必须用有效的、被Google信任的证书,别用自签名的。
- 参数没处理好:如果Dialogflow传了参数给Webhook,检查有没有参数缺失或者格式错误,导致服务没法生成完整响应。
六、最后测试验证
- 在Android设备上装你的应用,用
adb shell dumpsys package domain-preferred-apps命令看看App Links有没有验证通过。 - 要么用Actions Console的测试模拟器,要么直接对着Google Assistant说你配置的触发短语,看看能不能正确跳转到目标Activity。
- 要是用Dialogflow的Webhook,去Dialogflow的Diagnostic Info页面看请求和响应详情,能精准定位问题。
内容的提问来源于stack exchange,提问作者Hector
相关产品推荐
相关产品推荐

