Build a Hello World App for Android

Learning Objectives

After completing this unit, you’ll be able to:
  • Embed the Snap-ins SDK frameworks into your Android project.
  • Wire up some basic Snap-ins SDK features to your Android project.
  • Describe where to find more Snap-ins resources.

Get Started with Android

In this unit, we show you how quickly you can connect Service Cloud features to your existing Android app. It’s a bit different with the Android Snap-ins SDK than the iOS Snap-ins SDK. That’s because we’ve developed each SDK to follow the standard design patterns for each platform.

Note

Note

This unit assumes that you’re familiar with Android development. We’re not going to teach you how to write Java code or how to get around the Android Studio IDE. If you’re not familiar with these things, you can still scan the unit to get a feel for what it entails to use this SDK.

Create a Basic Project in Android Studio

Let’s create a phone project with Android Studio. Be sure to specify a Minimum SDK of API 19 or above.

Android studio setup

Also, let’s make a Basic Activity project, so that we get a floating action button (FAB) for free.

Basic activity project

Now we’re cooking. To bring in the SDK into our project, we need to update the repositories in the build.gradle script:

allprojects {
  repositories {
    google()
    jcenter()
    maven {
      url 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release'
    }
    maven {
      url 'http://tokbox.bintray.com/maven/'
    }
  }
}

And update the dependencies in the app’s build.gradle script:

dependencies {
  compile 'com.salesforce.service:servicesdk:+'
}
Note

Note

It is best to compile against a specific version of the SDK. Replace the "+" with the specific version you want to use. Check out the installation instructions for version information (see Resources).

Before we get to the code, let’s change the FAB from an email icon (ic_dialog_email) to a help icon (ic_menu_help). You can change this from activity_main.xml:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_menu_help"/>

Things are looking good so far. Now let’s add some code to this project so it does something interesting…

Let’s Get Coding…Android Style

In this unit, we’re once again going to hook into the Knowledge feature, but you can try any of the other features if you prefer.

First define some variables in our main Activity class.

// You should normally maintain a reference to these instances
// within the Application lifecycle (rather than Activity lifecycle)...
// They are placed here just for this example...
KnowledgeUI mKnowledgeUI = null;
KnowledgeUIClient mKnowledgeUIClient = null;

// Specify the community url, category group, and root category
public static final String communityUrl = "https://your-community-url";
public static final String categoryGroup = "your-category-group";
public static final String rootCategory = "your-root-category";

Like with the iOS unit, be sure to replace the placeholder values with real values for your community, data category group, and root data category. Check out Cloud Setup for Knowledge in the Resources section for detailed guidance.

And now let’s create an initialize method and a start method for Knowledge.

// Call this method at init time...
private void initKnowledge() {
    if (mKnowledgeUI == null) {
        // Create a core configuration instance
        KnowledgeConfiguration coreConfiguration =
            KnowledgeConfiguration.create(communityUrl);
        // Create a UI configuration instance from core instance
        KnowledgeUIConfiguration uiConfiguration =
            KnowledgeUIConfiguration.create(coreConfiguration, categoryGroup, rootCategory);
        // Create a UI instance
        mKnowledgeUI = KnowledgeUI.configure(uiConfiguration);
    }
}

// Call this method when you want to show the Knowledge UI
private void startKnowledge() {
    if (mKnowledgeUIClient == null) {
        // Create a client asynchronously
        mKnowledgeUI.createClient(MainActivity.this)
            .onResult(new Async.ResultHandler<KnowledgeUIClient>() {

            @Override public void handleResult (Async<?> operation,
                KnowledgeUIClient uiClient) {

                // Store reference to the Knowledge UI client
                mKnowledgeUIClient = uiClient;
                // Handle the close action
                uiClient.addOnCloseListener(new KnowledgeUIClient.OnCloseListener() {
                    @Override public void onClose () {

                        // Clear reference to the Knowledge UI client
                        mKnowledgeUIClient = null;
                    }
                });
              
                // Launch the UI
                uiClient.launchHome(MainActivity.this);
            }
        });
    }
}

The initKnowledge method uses configuration information to build a KnowledgeUI object. The startKnowledge method creates a KnowledgeUIClient from the KnowledgeUI object and then launches the interface with the SDK method, launchHome.

We can wire up the initKnowledge method to the onCreate method and the startKnowledge method to where the user taps the FAB. Something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    initKnowledge();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startKnowledge();
        }
    });
}

And that does it. Launch the app in the emulator, tap the FAB, and watch Knowledge appear.

Knowledge on Android

Like with iOS, this is just the beginning. You can add hero images and customize the colors to match your brand. You can build an entire support home experience that includes Knowledge, Case Management, Live Agent Chat, and SOS. And if you prefer, you can write your own UI and tap into our lower-level API.

What’s Next?

Check out the Resources section for links to handy documentation to take your service-enabled apps to the next level. You can start with the Snap-ins Developer’s Guide for Android. It contains more tutorials, more examples, and covers loads more topics. It gives you all you need to connect up the four major support features: Knowledge, Case Management, Live Agent Chat, and SOS. You can also refer to the Reference Documentation for Android for those nitty-gritty details about the API.

So there you have it—an introduction to Snap-ins for Mobile Apps. By now, you understand the value of this toolkit and have a feeling for what it’s like to use it. Lots of features, without much coding required. So now it’s your turn. Let’s see what you can do with this SDK!

retargeting