Page 1 of 1

Sprechen Sie mit meiner Android -App direkt mit Google Assistant, um eine einfache Aufgabe zu erledigen

Posted: 12 Sep 2025, 05:46
by Anonymous
Ich habe eine einfache App erstellt, um den Google -Assistenten zu integrieren, der als TestAppDemo bezeichnet wird. Shortcuthelper -Klasse < /p>
shortcutHelper = ShortcutHelper(this)

binding.btnCreateShortcut.setOnClickListener {
val itemName = binding.inputItem.text.toString().trim()
if (itemName.isNotEmpty()) {
shortcutHelper.pushDynamicShortcut(itemName)
Toast.makeText(this, "Shortcut created for \"$itemName\"", Toast.LENGTH_SHORT).show()
binding.inputItem.text.clear()
} else {
Toast.makeText(this, "Please enter an item name", Toast.LENGTH_SHORT).show()
}
}

// If started via shortcut, display incoming "item" parameter
intent.getStringExtra("item")?.let { item ->
Toast.makeText(this, "Opened via shortcut for item: \"$item\"", Toast.LENGTH_LONG).show()
}
< /code>
In meinem Shortcuthelper < /p>
fun pushDynamicShortcut(itemName: String) {
val shortcutId = "shortcut_$itemName"

val intent = Intent(context, MainActivity::class.java).apply {
action = Intent.ACTION_VIEW
putExtra("item", itemName)
}

val shortcut = ShortcutInfoCompat.Builder(context, shortcutId)
.setShortLabel(itemName)
.setLongLabel("Open \"$itemName\" from TestAppDemo")
.addCapabilityBinding(
"actions.intent.GET_THING",
"thing.name",
listOf(itemName)
)
.setIntent(intent)
.build()

ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
}
< /code>
Erstellte auch Verknüpfungen, um Funktionen zu verwenden. < /p>





< /code>
In der Manifestdatei < /p>








< /code>
Um dies zu erreichen, habe ich BII (in Absicht erstellen) verwendet. Zum Beispiel konnten wir nur sagen "Hey Google, öffnen Lebensmittel aus TestAppdemo", um die Aufgabe zu öffnen, aber es öffnet diesen Bildschirm nicht in der App, oder ich kann sagen, dass es nicht die Ausgabe angibt. TestAppDemo "Es sollte zu bestimmten Lebensmitteln navigieren.>