Android Quick Start
Steps to getting your first Android app integrated with Slang
Before you start
You need to have the following software installed on your system:
JDK version 8
Android Studio version 4.0 or higher
Android Platform API Level 16 or higher
Step 0: Setup
First off, open your Android app's project in Android Studio and prepare for Slang integration. For the remainder of this guide, we'll assume that the app is called MyAwesomeApplication.
Step 1: Add Slang library dependency
Open your project's module-level build.gradle file (Project Navigator -> Gradle Scripts -> build.gradle (Module: app).
Step 1a: Add the Slang repository:
Step 1b: Add the Slang library dependency
Using auto-generated SlangInterface.java
At this point, if you have received a copy of SlangInterface.java with all of the initialization boilerplate generated for your app, you're very close to completing the integration. Just drop the file into your app's Android project and make sure to initialize the interface from within your app's Application class, like this:
In SlangInterface.java, you will find empty methods like the following that represent actions that your app can perform:
You will need to replace the toast with the app logic necessary to complete the action for the appropriate intent. The Slang platform will ensure that the action is called automatically whenever a user speaks the corresponding utterance.
After implementing these action handlers, you can skip the following steps and directly jump to Step 5: Run the app.
Step 2: Gather credentials from Slang Console
If you haven't already configured your app's use cases in the Slang Console, please head to console.slanglabs.in and follow the steps in the Building Your First Buddy guide.
Once you've built a buddy corresponding to your app's use-cases, gather the API key and Buddy ID and have them handy for integration. For details on how to obtain these credentials from the Console, see here.
Step 3: Initialize Slang
To initialize Slang, you'll need to the applications Android context, the Buddy ID and API key gathered in Step 2. With these, you can initialize Slang in the following way:
With these code changes, when the app is run, Slang will be initialized and a movable Trigger will be automatically inserted on each activity of the app as it's created. Users of the app will now be able to click on this trigger to initiate the voice experience and start talking. But, by default, no action will occur after the user finishes speaking. In order for actions to occur, we need to write action handlers.
Step 4: Write action handlers
As users talk, Slang attempts to understand what they're saying and take appropriate action. Every utterance of each user that is understood by Slang will result in an "Intent" being recognized and the appropriate action for the Intent being called.
To understand the details behind Intents, please see the section on Intents in Buddy Building Tips.
In your app, you can write action handlers that specify what actions need to be performed when Intents are recognized by Slang. Recall that we passed an instance of this action handler during initialization time in Step 3.
Here is what an implementation of MyIntentAction could look like:
Step 4a: The "action" method
The "action" method of your SlangIntentAction implementation will be called every time an "Intent" is detected by Slang. This is where you can trigger functionality that exists in your app that corresponds to the detected intent/use-case. The logic can be as simple as launching an activity as shown here
Note that "Intents" being referred here are the intents/use-cases that were defined in the Slang Console. Not to be confused with the Android Intents!
Step 5: Run the app
Congratulations! That's all there is to integrating Slang with your app. Now, when you run your app, the trigger should appear automatically. Clicking on it and speaking an utterance configured for an app's use-case should perform the appropriate action.
Digging Deeper
While we've looked at a quick and easy way to get started with integrating Slang into your app, Slang provides many more features and options to control the overall voice experience for your app. To learn more about these features, options and APIs, please head to the Android Deep Dive section.
Last updated