Add AI Voice Assistant to Android Apps

Create a new project with Basic Activity
Naming the Project
MainActivity.java
Create an empty project
Add predefined scripts
dependencies {
...
// Add Alan dependency
implementation 'app.alan:sdk:4.12.0'
}
Module level build.gradle
<com.alan.alansdk.button.AlanButton
android:id="@+id/alan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="5dp"
android:layout_marginBottom="70dp"
android:visibility="visible"
app:button_horizontal_align="right" />
activity_main.xml
public class MainActivity extends AppCompatActivity {
...
private ActivityMainBinding binding;
private static final String SDK_KEY = "YOUR SDK KEY"; @Override
protected void onCreate(Bundle savedInstanceState) {
... AlanButton alanButton = binding.alanButton;
AlanConfig config = AlanConfig.builder()
.setProjectId(SDK_KEY).build();
alanButton.initWithConfig(config);
}
}
Hello world
intent('What can you do?', p => {
p.play('I can add a task and read them for you');
});intent('Say hurrah', p => {
p.play('Hurrah!');
});
Custom voice command

“Contain as much code in respective methods so that it gets easier to add voice in later steps”

//In Alan console
intent('(Go|take me) back', p => {
p.play('Sure');
p.play({ commandName: "go_back" });
});intent('Add a todo task (named|with title|) $(TASK_TITLE* (.+))', p => {
p.play('Adding the task');
p.play({ commandName: "add_todo", title: p.TASK_TITLE.value });
});
Alan Studio console
AlanCallback alanCallback = new AlanCallback() {
/// Handle commands from Alan Studio
@Override
public void onCommand(final EventCommand eventCommand) {
try {
JSONObject command = eventCommand.getData();
1 //data object is the JSON Object returned in p.play() method
JSONObject data = command.getJSONObject("data");
String commandName = data.getString("command");
//based on commandName we can perform different tasks
executeCommand(commandName, data);
} catch (JSONException e) {
Log.e("AlanButton", e.getMessage());
}
}
};
alanButton.registerCallback(alanCallback);
private void executeCommand(String commandName, JSONObject data) {
if (commandName.equals("go_back")) {
onBackPressed(); //android lifecycle method
}
if (commandName.equals("log_out")) {
logOut();
}
if (commandName.equals("add_todo")) {
try {
String title = data.getString("title");
addTodoItem(title);
} catch (JSONException e) {
Log.e("AlanButton", e.getMessage());
alanButton.playText("I'm sorry I'm unable to do this at the moment");
}
}
}private void logOut() {
//code to log out
}private void addTodoItem(String title) {
//code to add a Todo Item
}
MainActivity.java

What else can you do with Alan?

//Alan script
projectAPI.greetUser = function(p, param, callback) {
p.userData.nameOfUser = param.userName;
p.play(`Hey ${p.userData.nameOfUser}, this is Alan. The voice assistant for Todo app. Here, you can store your daily tasks and prioritize them`);
};
public void greetUser(String name) {
binding.alanButton.activate();//parameters to be passed
JSONObject object = new JSONObject();
try {
object.put("userName", name);
binding.alanButton.callProjectApi("greetUser", object.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
private void speakText(String text, AlanButton alanButton) {
//if the function is written outside MainActivity.java
//pass alanButton in parameter as shown and instead write
//alanButton.playText(text); binding.alanButton.playText(text);
}
Handler Methods

Don’t forget to give us your 👏 !

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store