Blog Infos
Author
Published
Topics
Published
Topics

When you get a pizza craving, what do you do first? “Hey, Google, get my favorite pizza,” do you tell it? This is just one example of how Google Assistant has made our lives easier. Google Assistant is now available on a wide range of Android devices, including smartphones, tablets, watches, and even automobiles! As a developer, you may use App Actions to integrate Google Assistant into your own app.

Google Assistant may access your app and do specified activities within it using App Actions. This article will show you how to add App Actions to your app so that users may interact with it using Google Assistant.

Note: This article assumes you know the fundamentals of Android development in Kotlin. You should be familiar with deep links as well. To test App Actions, you’ll also need a physical device that runs API 21 or above.

Table of contents:
  • Introducing App Actions
  • Understanding the Internal Working of App Actions
  • Source Code
  • Adding Your First App Action
  • Identifying the Correct BII
  • Creating actions.xml File
  • Referencing the App Action in Manifest File
  • Uploading a Draft App to Play Console
  • Testing App Actions
  • Using the App Actions Test Tool
Introducing App Actions

Shortcuts to your app are known as App Actions. They can use Google Assistant to launch different aspects of your app. With a simple voice command like “Hey, Google, tell me the stock quote of GOOGL in Stock Tracker,” you may deep link with your app.

Any information from your app can also be displayed directly in the Google Assistant discussion dialogue using App Actions. You don’t have to open your app manually; ask Google Assistant to fetch it for you without switching contexts.

Understanding the Internal Working of App Actions

Intents are the foundation of App Actions. Intents are objects that can ask for actions from various app components. For triggering distinct App Actions, there are two categories of intent:

  • Built-in intents (BIIs): BIIs are a set of Google intents that enable common app functionality, as the name implies. BIIs are divided into several groups actions.intent.GET_NEWS_ARTICLEactions.intent.CREATE_CALL, and actions.intent.ORDER_MENU_ITEM are instances of built-in intentions.

The official index contains a complete list of built-in intents. In the actions.xml file, you must define which built-in intent you wish to use for your project.

  • Custom intents: Although there are presently over 60 built-in intents, some elements of your app may require functionality that isn’t yet available. You can use custom intents to deal with this situation.

The key difference between a built-in and a custom intent is that a custom intent requires additional query patterns to include what the user may say. Within a string array, you must provide the various query patterns. There are a maximum of 100 query patterns for each custom intent you implement in your app.

Google Assistant matches the user query with your built-in (or custom) intent when you add an intent to your App Action. The intent parameter is extracted, passed to the determined fulfillment for that intent, and the user is then deep linked within the app.

Note: Deep links invoked by Google Assistant should already be handled by the app.

Source Code

Get the project here and Import the project into Android Studio.

Take a few moments to become acquainted with the code. The following classes are available:

  • MainActivity.kt: The app’s home screen, where users can enter their favorite stock symbol. It also manages the deep linkages that App Actions trigger.
  • Stock.kt: This file contains the data classes that are utilized to parse the API response.
  • StockApiService.kt: This file contains all networking-related functionality, including the GET endpoint for retrieving stock quotations. It also exposes a StockQuoteActivity.kt retrofit object.
  • StockQuoteActivity.kt: The app’s second screen, which shows the most recent information about the selected stock. Coroutines are asynchronously fetching data from the network in this class.

Note: You must add your own API key before you can build and execute the app. Alpha Vantage Support can provide you with your API key. To assign the value to the constant named API_KEY, open StockQuoteActivity.kt and put the key inside the empty quotes.

Now you’re all set. Run the app.

Adding Your First App Action

Three steps are required to begin adding App Actions:

  • Determine which built-in intent to utilize.
  • Make a file called actions.xml.
  • Allow the app to see your App Actions.
Identifying the Correct BII

Determine which of your app’s main functions you want to define App Actions for. If it’s a food delivery service, for example, the major feature will be ordering food (no surprises there!). Then look through this list of built-in-intents to choose the right one for your use case.

StockTracker pulls the most recent stock quotes from the internet for this article. actions.intent.GET_STOCK_QUOTE is the correct BII for this use case. Each stock is identified by a single purpose parameter called tickerSymbol.

Creating actions.xml File

In app/src/main/res/xml, create a new file called actions.xml.

Note: If an xml folder does not already exist, make one by right-clicking the res directory and selecting New Android Resource Directory. Select xml as the Resource type and xml as the Directory name. Then press OK.

The following code below the xml tag in the newly formed actions.xml file:

<actions>
// 1:
<action intentName="actions.intent.GET_STOCK_QUOTE">
// 2:
<fulfillment urlTemplate="app://stocktracker/query{?symbol}">
// 3:
<parameter-mapping
intentParameter="tickerSymbol"
required="true"
urlParameter="symbol" />
</fulfillment>
// 4:
<!-- Fallback fulfillment when no parameter supplied -->
<fulfillment urlTemplate="app://stocktracker/home" />
</action>
</actions>
view raw actions.xml hosted with ❤ by GitHub

In the xml code above, you’re:

  • Using actions to create a new action. The action tag, intent.GET_STOCK_QUOTE: action, is necessary and defines the App Action you want to use. A unique action tag will be assigned to each App Action. This action’s built-in intent is called intentName.
  • Creating a new fulfillment for the appropriate action: fulfillment tells us how the supplied intent fulfills the request. urlTemplate creates deep links according to the AndroidManifest.xml specification. App://stocktracker is the basic deep link for fulfillment, and /query?symbol is the path that takes in a query argument named symbol.
  • Making a new parameter mapping for the query parameter mentioned above: The query parameters in the URL template are mapped to intent parameters using parameter-mapping. This mapping is necessary since the tickerSymbol intent parameter of actions.intent.GET STOCK QUOTE is different from the symbol query parameter. The symbol value must be present for the URL to be valid.
  • Adding a new fallback fulfillment: urlTemplate creates a new deep link app:/stocktracker/home in this fulfillment, where /home is the route. This fulfillment occurs when a user launches an intent without specifying a symbol in the query parameter.

Note: Defining a fallback fulfillment to handle different edge circumstances is always a smart idea. When a user doesn’t supply correct input, you don’t want them to have a bad experience.

The program already handles the deep link pathways mentioned above in handleDeepLink() in MainActivity.kt ().

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

Put Your Tests on a Diet:Testing the Behavior and Not the Implementation

How do you write tests? How much time do you spend writing tests? And how much time do you spend fixing them when refactoring?
Watch Video

Put Your Tests on a Diet:Testing the Behavior and Not the Implementation

Stelios Frantzeskakis
Staff Engineer
PSS

Put Your Tests on a Diet:Testing the Behavior and Not the Implementation

Stelios Frantzeska ...
Staff Engineer
PSS

Put Your Tests on a Diet:Testing the Behavior and Not the Implementation

Stelios Frantzes ...
Staff Engineer
PSS

Jobs

Referencing the App Action in Manifest File

Just before the closing application tag, open AndroidManifest.xml and you see the following code:

<meta-data
android:name="com.google.android.actions"
android:resource="@xml/actions" />

The preceding line of code ensures that the app can see your actions.xml file. If you named your actions file something else, you’ll need to alter the android: resource attribute.

Congratulations on successfully adding your first App Action! It’s now time to put your App Action to the test.

Uploading a Draft App to Play Console

You must first upload a draft version of your app to Play Console before you can begin adding App Actions. You can now use the App Actions Test Tool, which is discussed later in this article.

Note: This article assumes you already have a Google account with Play Console access.

To begin, open app/build.gradle and replace applicationId with a unique one. Set applicationId to com.youruniquename.android.stocktracker, for example. This will ensure that your app is successfully uploaded.

Then, in Android Studio, build your signed app and publish it as an internal release.

Testing App Actions

A physical device running API 21 or higher is required to test App Actions. It may have unexpected results if you try it on a virtual emulator. Connect your actual device to your development machine on your local network.

Preparing for Testing

Ascertain that your device is connected to the internet and that Google Assistant is already installed and operational. Press the home button repeatedly. If you see a screen that looks like the one below, you’re good to go.

image by author

Sign in to Android Studio using the same Google account that you used to log in to your physical device. This is the same account that you used to upload the draft app.

In Android Studio 4.0.0 or later, click the white user icon in the top-right corner. Follow the on-screen directions. By clicking the user icon once you’ve signed in, you may double-check that your email address is correct.

Next, install the App Actions Test Tool plugin by going to File ▸ Settings (Android Studio ▸ Preferences on MacOS). Select the Plugins section on the left side and search for App Actions Test Tool in the marketplace.

image by author

Finally, it’s time to try out your new plugin.

Using the App Actions Test Tool

The App Actions Test Tool is an Android Studio plugin for testing App Actions. During the development process, it’s a really useful tool. It works by parsing the actions from the actions.xml file you produced earlier to create a preview of your App Action.

Open the App Action Test Tool by going to Tools ▸ App Actions ▸ App Actions Test Tool. You’ll see a screen that looks like this:

image by author

Enter Stock Tracker in the Invocation name field. Your App Action is triggered by this invocation name. Leave the default value for Locale. Create a preview by clicking the Create Preview button. You’ll see the screen following after a successful preview creation:

image by author

It’s worth noting that it understands the built-in intent you specified in actions.xml. There’s also a tickerSymbol area where you can make adjustments. Check that your target device is visible in this window.

To put your App Action to the test, click Run. It activates Google Assistant on your device before deep linking into the app to provide the right stock quote for the tickerSymbol value.

Try manually activating Google Assistant by saying, “Hey, Google, tell me the stock price of Tesla in Stock Tracker.” After a few seconds, the screen below appears, followed by StockTracker, which displays the most recent quote.

image by author

Play around with your App Actions by experimenting with different stocks in Google Assistant. Say “Hey, Google, tell me the current stock price in Stock Tracker” to test the fallback fulfillment. Because you didn’t provide any stock this time, it uses fallback fulfillment and displays a welcome message on the app’s home screen.

image by author

You may now share your App Action with the public after you’ve designed and tested it.

You can find the source code here.

Thank you for taking the time to read this article. If you found this post to be useful and interesting, please clap and recommend it.

If I got something wrong, mention it in the comments. I would love to improve.

This article was originally published on proandroiddev.com on May 27, 2022

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Every good Android application should be well tested to minimize the risk of error…
READ MORE
blog
In this article we’ll go through how to own a legacy code that is…
READ MORE
blog

Running Instrumented Tests in a Gradle task

During the latest Google I/O, a lot of great new technologies were shown. The…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu