如何通过Google Assistant触发Android应用内的Toast动作并获取反馈
实现Google Assistant触发Android应用自定义Toast的完整方案
嘿,这个需求其实用Google的App Actions就能完美实现,我给你一步步拆解,保证能搞定:
1. 配置App Actions核心映射文件(actions.xml)
首先得让Google Assistant知道你的语音指令对应应用里的哪个动作,这就需要在项目的res/xml目录下创建actions.xml文件,用来定义指令和应用动作的映射:
<?xml version="1.0" encoding="utf-8"?> <actions:actions xmlns:actions="http://schemas.android.com/apk/res-auto"> <!-- 自定义Capability,对应你的Toast触发动作 --> <actions:capability name="actions.intent.CUSTOM_DISPLAY_TOAST"> <!-- 配置触发后的应用跳转逻辑 --> <actions:fulfillment intentName="actions.intent.CUSTOM_DISPLAY_TOAST"> <actions:uriTemplate>mytoastapp://displaytoast</actions:uriTemplate> </actions:fulfillment> <!-- 设置Assistant能识别的语音触发短语,要覆盖你想要的指令 --> <actions:trainingPhrase> <actions:example>display toast from my toast app</actions:example> </actions:trainingPhrase> <actions:trainingPhrase> <actions:example>show toast from my toast app</actions:example> </actions:trainingPhrase> </actions:capability> </actions:actions>
接着要在AndroidManifest.xml里注册这个Deep Link的处理组件,比如用一个无界面的Activity:
<activity android:name=".ToastTriggerActivity" android:exported="true" android:theme="@android:style/Theme.NoDisplay"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 匹配我们定义的Deep Link Scheme --> <data android:scheme="mytoastapp" android:host="displaytoast" /> </intent-filter> </activity>
2. 实现应用内Toast触发与结果返回
接下来在ToastTriggerActivity里处理Assistant的跳转请求,显示Toast并给Assistant返回成功信号,这样它才能回复用户:
class ToastTriggerActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 不需要界面,处理完直接关闭Activity finish() // 显示目标Toast Toast.makeText(this, "hello world", Toast.LENGTH_SHORT).show() // 给Assistant返回成功结果,让它知道动作完成 val resultIntent = Intent().apply { putExtra("actions.intent.CUSTOM_DISPLAY_TOAST_RESULT", "success") } setResult(RESULT_OK, resultIntent) } }
如果你觉得用Activity太重量级,也可以换成BroadcastReceiver,逻辑类似:
class ToastReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { Toast.makeText(context, "hello world", Toast.LENGTH_SHORT).show() // 异步返回结果给Assistant(BroadcastReceiver需要用goAsync) val pendingResult = goAsync() Handler(Looper.getMainLooper()).post { val resultIntent = Intent().apply { putExtra("actions.intent.CUSTOM_DISPLAY_TOAST_RESULT", "success") } pendingResult.setResultCode(RESULT_OK) pendingResult.setResultData(resultIntent) pendingResult.finish() } } }
记得在Manifest里注册这个Receiver替换掉之前的Activity配置哦。
3. 关联Google Cloud项目并配置Assistant回复话术
现在需要让Assistant识别你的自定义指令,这一步要用Android Studio的App Actions Test Tool:
- 打开Android Studio,点击顶部
Tools > App Actions > App Actions Test Tool - 登录你的Google账号,选择或创建一个和应用包名绑定的Google Cloud项目
- 导入刚才写的
actions.xml,然后找到CUSTOM_DISPLAY_TOAST动作,配置成功场景的回复话术为toast displayed successfully - 点击「Deploy to test」,把配置推送到你的测试设备
4. 测试功能
确保测试设备登录了同一个Google账号,并且安装了你的应用:
- 让手机处于闲置状态,说出
Hello Google, display toast from my toast app - 你会看到应用被唤醒,弹出"hello world"的Toast,随后Assistant回复
toast displayed successfully
一些注意事项
- Android 12及以上版本,需要在Manifest里添加
QUERY_ALL_PACKAGES权限,或者配置Deep Link验证,避免系统拦截跳转 - 如果测试时Assistant没反应,可以先手动打开Deep Link(比如在浏览器输入
mytoastapp://displaytoast),确认应用能正常触发Toast - 训练短语可以多写几个,覆盖不同的表述方式,提升Assistant的识别率
内容的提问来源于stack exchange,提问作者Vaibhav Khandekar
相关产品推荐
相关产品推荐

