如何通过Google Assistant实现Android App特定页面的深度链接?
当然可以实现!这正是Google Assistant的App Actions功能专门解决的场景,完全能帮你实现用户通过语音指令直接跳转到新闻App对应内容页的需求~
核心实现方案:App Actions + 深层链接/Intent映射
简单来说,就是通过定义语音指令与App内部页面的关联规则,让Google Assistant把用户的语音请求转化为打开指定Activity的操作,具体步骤如下:
1. 引入App Actions依赖
首先在你的Module级build.gradle中添加依赖,让App能对接Google Assistant的能力:
dependencies { implementation 'com.google.android.gms:play-services-appactions:16.0.0' }
2. 定义语音指令与App的映射规则
在res/xml/目录下创建actions.xml文件,这里可以用Google提供的**内置意图(比如GET_NEWS)**直接匹配用户的新闻类请求,也可以自定义专属指令:
<?xml version="1.0" encoding="utf-8"?> <actions:actions xmlns:actions="http://schemas.android.com/apk/res-auto"> <!-- 匹配"打开XX新闻的科技新闻"这类请求 --> <actions:action intentName="actions.intent.GET_NEWS"> <!-- 方式1:通过自定义Scheme深层链接跳转 --> <actions:fulfillment urlTemplate="mynewsapp://news/{topic}"> <actions:parameter-mapping intentParameter="news.topic" urlParameter="topic" /> </actions:fulfillment> <!-- 方式2:直接指定启动目标Activity的Intent --> <actions:fulfillment intentName="android.intent.action.VIEW"> <actions:parameter-mapping intentParameter="news.topic" appParameter="topic" /> <actions:extra key="android.intent.extra.REFERRER_NAME" value="com.google.android.googlequicksearchbox" /> </actions:fulfillment> </actions:action> </actions:actions>
3. 配置目标Activity的Intent Filter
在AndroidManifest.xml中给你的新闻内容Activity添加对应的意图过滤器,确保它能响应Google Assistant发来的请求:
<activity android:name=".ui.NewsCategoryActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 匹配自定义Scheme的深层链接 --> <data android:scheme="mynewsapp" android:host="news" android:pathPattern="/.*" /> </intent-filter> </activity>
4. 接收参数并加载内容
在目标Activity中,你可以通过Intent获取传递的新闻主题参数,然后加载对应内容:
class NewsCategoryActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 获取从Assistant传递来的新闻主题 val newsTopic = intent.data?.lastPathSegment ?: intent.getStringExtra("topic") // 根据topic加载对应分类的新闻内容 loadNewsByTopic(newsTopic) } }
5. 测试与优化
- 你可以用
adb命令快速测试映射是否生效:adb shell am start -a android.intent.action.VIEW -d "mynewsapp://news/technology" - 也可以直接在设备上对着Google Assistant说出指令(比如“嘿Google,打开我的新闻App的科技新闻”)进行真实场景测试。
- 如果需要更精准的指令匹配,还可以自定义意图,比如针对“今日头条”“财经快讯”这类专属场景设置特定的语音触发词。
内容的提问来源于stack exchange,提问作者Hassan Imtiaz




