确保使用了Android 6.0(Marshmallow)或更高版本的设备,并且已经给与了Google Assistant所需的权限。
使用以下代码来触发自定义动作:
String json = "{\"@type\": \"type.googleapis.com/google.actions.v2.OptionValueSpec\","
+ " \"carouselSelect\": {"
+ " \"items\": [{"
+ " \"optionInfo\": {"
+ " \"key\": \"YOUR_OPTION_KEY\","
+ " \"synonyms\": [\"YOUR_OPTION_SYNONYMS\"]"
+ " },"
+ " \"title\": \"YOUR_TITLE\","
+ " \"description\": \"YOUR_DESCRIPTION\","
+ " \"image\": {"
+ " \"url\": \"YOUR_IMAGE_URL\","
+ " \"accessibilityText\": \"YOUR_IMAGE_ALT_TEXT\""
+ " }"
+ " }]"
+ "}}";
String SELECT_INTENT = "actions.intent.OPTION";
Intent intent = new Intent(SELECT_INTENT);
intent.putExtra("OPTION", json);
sendBroadcast(intent);
请注意,上述代码中的“YOUR_OPTION_KEY”和“YOUR_OPTION_SYNONYMS”需要替换为自定义选项的值。另外,“YOUR_IMAGE_URL”和“YOUR_IMAGE_ALT_TEXT”需要替换为要使用的图像的URL和替代文本。
public class MyCustomActionHandler {
public static final String ACTION_MY_CUSTOM = "{your.package.name}.action.YOUR_ACTION";
public void handle(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_MY_CUSTOM.equals(action)) {
//处理选项
String optionKey = intent.getStringExtra("OPTION");
if (optionKey.equals("YOUR_OPTION_KEY")) {
//执行自定义动作
}
}
}
}
上一篇:Android上的语音识别问题
下一篇:Android上的自定义阴影