Build a Hello World App for Android
Learning Objectives
- Embed the Service SDK frameworks into your Android project.
- Wire up some basic Service SDK features to your Android project.
- Describe where to find more Service SDK 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 Service SDK than the iOS Service SDK. That’s because we’ve developed each SDK to follow the standard design patterns for each platform.
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 21 or above.
Also, let’s make a Basic Activity project, so that we get a floating action button (FAB) for free.
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:+' }
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.
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, and Chat. 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 Embedded Service SDK for Android Developer Guide. It contains more tutorials, more examples, and covers loads more topics. It gives you all you need to connect up the major support features: Knowledge, Case Management, and Chat. 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 the Embedded Service SDK 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!
Resources
- Embedded Service SDK for Android Developer Guide
- Android SDK Installation Instructions (from the Developer Guide)
- Cloud Setup for Knowledge (from the Developer Guide)
- Embedded Service SDK Reference Documentation for Android
- Embedded Service SDK Developer Center
- Android
- Android Studio