Content Management SDK for Java & Android

Published on December 15, 2014

knowledge-base-white-paper

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

With the current hype around wearables, you're probably tired of hearing about them already. When we discovered this new way of managing content for Android devices though, we got quite excited!

The most traditional use of wearables is definitely consumption-based (i.e. browsing cards, checking notifications, etc.), but we want to ensure that you’re not restricted in adapting to more futuristic ideas.

With that in mind, we are happy to announce the Java SDK for our Content Management API (CMA).

The CMA SDK allows you to easily create and update your content from anywhere using Java. This opens a huge door for developers, making it possible to shape their content from any context, whether it be a mobile phone, tablet or even a wearable device.

In this blog post, we will take you through the SDK and our demo Fortune Cookie app, which turns your speech into entries in Contentful!

CMA Cookies Demo

The source code for the SDK is available on GitHub.

Setup

The binaries for the library are published via Maven Central, and can be added to your project in several ways, depending on how you usually build your project.

Maven

Gradle

The latest version number and more details are available in the Setup section of the project's README file.

Creating a client

The CMAClient class manages all your interaction with the Management API. Every client is associated with an access token, which can be obtained through the management API documentation.

Creating a client is as simple as:

There are also several optional settings that can be specified through the client builder:

setLogLevel() - Configures a custom log level for the client. This can be useful for debugging purposes, as it outputs the network traffic according to the provided log level.

setClient() or setClientProvider() - Configures a custom HTTP client to carry the network requests.

Examples

Creating an entry:

Updating an entry:

Demo

Next we will demonstrate basic usage of the SDK from the context of an Android Wear project. For that, we have published the [Cookies Demo Application for Android][9].

CMA Cookies Demo

Implementation

As you probably know, Android Wear applications normally require two projects - one that would run on the handheld device and another for the wearable side. Hence, the demo project is structured as follows:


ROOT FOLDER
.. mobile/ -- Application module for handheld device
.. wear/ -- Application module for wearable device
.. shared/ -- Library project shared between both modules

The demo app makes use of the CMA SDK in order to create and publish entries. The CDA SDK is used for fetching random entries from the same Space.

The wearable project's MainActivity class has a layout consisting of two buttons. One for requesting a fortune cookie via the Delivery API and the other for storing new fortune cookies via the Management API. The text for the new fortune cookie will be retrieved from the user by voice, using an ACTION_RECOGNIZE_SPEECH Intent.

Since network operations should be performed by the handheld device, once one of the buttons is clicked, we deliver a message to the handheld device via the Wearable MessageApi. The message will be intercepted by a custom WearableListenerService, which in turn fetches or creates a fortune cookie according to the desired action. The result will be sent back to the wearable device by using the same mechanism.

In order to send a message from one side to the other (i.e. from the wearable to the handheld), we create a GoogleApiClient. Once the client is connected and ready, we should be able to send messages. A message consists of a path and an optional payload. We have declared the following method (from Utils.java) to send a message to other connected nodes:

Note that since we are using the synchronous versions of the getConnectedNodes() and sendMessage() methods, we spawn a new thread in order keep the main thread free.

On the handheld side, we have a custom WearableListenerService which intercepts these messages and delegates the work to an IntentService named CookieService. The following method will be invoked on a background thread when receiving a message for creating a new fortune cookie:

Here we simply create and publish a new Entry with our pre-defined Content Type and set it's **text** field value. If the operation was successful, we notify the wearable by firing yet another message. In case the operation has failed for any reason (i.e. network error), we fire a message with the error description.

On the wearable side, we have a custom **WearableListenerService** which intercepts these message and reacts accordingly:

Conclusion

If you haven't had a chance to develop for Android wear yet, hopefully this would show you how to start. The full source code for the demo app is available on GitHub. If you have any feedback about the SDK, feel free to communicate it through the GitHub repository.

Happy coding q:)

Credits

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Related articles

Live preview enables teams to see their content in context and optimize in real-time, so creation comes to life as envisioned for a variety of experiences.
News

See your content come to life with live preview

May 22, 2023

Announcing the launch of two new courses tailored to help content creators, marketers, designers, and developers harness the power of Contentful Studio.
News

Enroll in our new Contentful Studio courses for authors and developers

May 20, 2024

Welcome to the Integration Roundup, where we spotlight new integrations added to the Contentful Marketplace and the terrific benefits they bring to users.
News

Integration Spotlight: Speak every customer’s language with Phrase Localization Platform

November 18, 2024

Contentful Logo 2.5 Dark

Ready to start building?

Put everything you learned into action. Create and publish your content with Contentful — no credit card required.

Get started