Skip to end of metadata
Go to start of metadata

The AMPLIFY Appcelerator Platform Services SDK for Android provides APIs for your Android application built with Java to access Mobile Backend Services (MBS).

Getting the SDK

To download and start using the SDK, you first need to register your Android application in Dashboard. See Managing Non-Titanium Client Applications in Dashboard for details on registering a new application. After you register the application, a service key is generated that associates your application with all the Platform services. Appcelerator Dashboard also provides full instructions for enabling all Platform Services in your application. This guide will deal specifically with enabling and using MBS in an Android application.

Running the APSCloud example application

The SDK ZIP file includes an Android sample project that demonstrates the basic usage of each of the Cloud APIs. To run the sample, you first need to register a new application in Dashboard to obtain the necessary service key. You will then copy the key into the imported sample project's main Activity and then run the application.

To create the APSCloudExample application in Dashboard:

  1. Log into the AMPLIFY Platform.
  2. Select the Dashboard link on the Dashboard tile.
  3. From the Orgs menu, select the organization to associate with the application. Note that the Orgs menu will not appear if you are not a member of multiple organizations.
  4. Click the Add menu (+) and select Register App for Services.
  5. In the dialog:
    • Type APSCloudExampleApp (or another name) in the Name field.
    • Select Android from the Platform menu.
    • Select any category from the Category menu.
  6. Click Next and then click the Overview tab.
  7. Click the Services tab, then click Show Key under Cloud > Performance > Analytics.
  8. Select Development from the Environment menu, then click the clipboard icon to copy the key to your clipboard.

Next, import the APSCloudExample project into Eclipse, copy the key from your clipboard into the application's main activity, and run the application.

To import the completed APSCloudExample project:

  1. In Eclipse, select File > Import > General > Existing Code into Workspace, then click Next.
  2. Click Browse and navigate to the appcelerator-sdk-android-<VERSION>/examples/APSCloudExample folder, and click Open.
  3. Click Finish.
  4. Open the src/main/java/com/appcelerator/apscloudexample/ file.
  5. Locate the following line of code and replace << YOUR APP KEY >> with the application key you copied to your clipboard previously.

  6. Run the application on an Android device or emulator.

Once the application is running, try the following:

  • Create a new user by selecting Users > Create User. Enter a username, password, and password confirmation, then click Create. If the user is created successfully, the following dialog is shown:
  • View the newly created user in Dashboard:
    1. Log into the AMPLIFY Platform.
    2. Select the Dashboard link on the Dashboard tile.
    3. Select your MBS application from the Apps tab.
    4. Select Manage Data, then click Users in the Manage Data Objects table. You should see the user you created listed in the Users table.

Enabling Cloud services in a new project

Once you've registered an application in Dashboard, downloaded the SDK, and obtained your application service key, there are few steps to enable Cloud services in your Android project.

To enable the Cloud services in your project:

  1. Copy the appcelerator-sdk-android-<VERSION>.jar file to your project's libs folder.
  2. Add the following permission to your project's AndroidManifest.xml file:

  3. Import the APSServiceManager class into the project's main Activity:

  4. Call APSServiceManager.getInstance().enable(), passing it the application context and the application key provided by Dashboard:

    At this point, your application can begin making API calls. Note that the application will need to import additional classes, depending on which APS APIs it uses.

Making API Calls and Handling Responses

The com.appcelerator.aps package contains a collection of classes whose methods map to individual REST API method endpoints. For example, the APSUsers.create() method corresponds to the /users/create.json method endpoint.

Alternatively, you can use the generic APSCloud.sendRequest() method to make REST calls directly against the Cloud APIs. For more information, see Making Generic REST API Calls.


All Cloud API calls must be made on the UI (main) thread, and callbacks are executed on the UI thread.

Building Request Parameters

The first parameter of each Cloud API method is a HashMap object that contains the parameters to send with the request. For example, the method takes a photo_id parameter whose value is, naturally, the ID of the photo to show.

Handling Responses

The second parameter of each method call is an instance of APSResponseHandler, an interface that has the following signature:

The instance you specify must override the onResponse and onException methods. The onResponse method is invoked upon completion of a Cloud API call, and the onException handler is invoked if there is an exception while communicating with the MBS server.

The APSResponse object provides getter methods to access information about the response. For instance, the getSuccess() method returns a boolean indicating if the method call was successful or not; the getResponse() method returns a JSON-encoded object with the results of the method call.

The onException() handler is invoked for any exceptions that occur during communication with the MBS server.

Example: APSUsers Login Call with Response Handler

The following example logs in an existing MBS user by their username and password. After a successful login, the application updates a TextView object with the user's MBS username.

 Expand source

Monitoring Request Progress

For Cloud API methods that involve uploading large files, such as APSPhotos.create() or APSFiles.create(), there is an overloaded version that takes an optional progressHandler parameter. This parameter takes an APSProgressHandler instance, which must provide an onProgress handler. This handler is periodically triggered as the file transfer continues and is passed an integer between 0-100, indicating the current upload progress.

Example: APSFiles Create Call with Progress Handler

The following example uploads a file from the device (/res/raw/reference.pdf) to the MBS storage server. Since the method call requires that uploaded data be an instance of, the application needs to copy the resource to a read-write directory before uploading it. Locally storing the file requires that the WRITE_EXTERNAL_STORAGE permission is included in your AndroidManifest.xml file.

The progress callback calls the setProgress() method on a ProgressBar object, displaying the status of the upload. After the request completes, the application displays a toast notification.

 Expand source

Making Generic REST APIs Method Calls

The APSCloud.sendRequest() method lets you quickly make REST API calls directly against MBS, rather than using the specialized classes (like APSUsers). In general, you should use specialized classes as they provide an easier API. However, if new REST methods are deployed to the APS Cloud backend, this approach lets you immediately start using those methods without waiting for an update to the SDK.

To make a generic request, you call APSCloud.getInstance() to get a reference to the shared APSCloud object and call its sendRequest() method. For each call, you must specify the following:

  • REST API method endpoint relative to Method endpoints are listed in the corresponding entries in the REST API documentation.
  • The HTTP method to use.
  • Data to send with the request.

For example, to create a post, pass the sendRequest() method the following information:

  • REST API method endpoint: posts/create.json
  • The HTTP method to use: POST
  • Data to send with the request: at a minimum, you must specify the content property.

The following uses the sendRequest() API to create a new Post object.

 Expand source

Working with Push Notifications

The  APSPushNotifications class lets your application subscribe, send, and receive push notifications. To use the class, you also need the  APSCloudPush class, which provides the underlying services to handle incoming push notifications.

To use these classes:

Android Project Requirements for using APSCloud Push

Once you have configured your Google Cloud Messaging (GCM) or Firebase Cloud Messaging (FCM) settings in Dashboard, there are some required configuration changes to your Android project to use APSCloudPush. Firebase Cloud Messaging (FCM) is the new version of GCM.

Add Google Play services

APSCloudPush requires that Google Play services be included in your application.

  1. Download the Google Play services SDK using the Android SDK Manager.
  2. Copy the <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib folder to the same workspace folder as your Android projects (not into your Android project).
  3. Add the following inside the  <application/>  element of your AndroidManifest.xml file:

For Android Studio projects, update the dependencies field of the build.gradle file, then save and sync the gradle file.

For Eclipse projects:

  1. Import the library project into Eclipse. (From the menu, click File > Import, then select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it.)
  2. In the application project, reference the Google Play services library project. Right-click the project folder and select Properties, then select Android, click Add.. and select the library project.

For other projects, update the file to reference the library project:

For detailed directions, see Android Developer: Setting Up Google Play Services.

AndroidManifest changes

The following changes must be added to your project's AndroidManifest.xml file to use APSCloudPush. Replace each occurrence of "YOURAPPSPACKAGENAME" with the actual package name of your application.

  • Inside the <manifest/> element:

  • Inside the <application/> element:

     Expand source

Subscribe to push notifications

Once the project is set up, the application needs to register with MBS to receive push notifications. The application should do this once the application starts, for example, in the onCreate() method of the application's main activity.

To register push notifications, the application needs to retrieve the device token using the  APSCloudPush.getInstance.retrieveDeviceToken() method, then pass the token to either the  APSPushNotifications.subscribe() or  APSPushNotifications.subscribeToken() method to subscribe to a push channel.


Call APSServiceManager.getInstance() before calling any methods on APSCloudPush; otherwise, an exception will be thrown.

For example, the code below can be added to the main activity's onCreate() method to subscribe the device to the friend_channel:

 Expand source

Once push services have been configured, and you've obtained a device token by registering your application to receive push notifications, you can start calling methods of the APSCloudPush and APSPushNotifications classes.

CloudPush sample application

The SDK includes the APSCloudPushExample application that demonstrates the use of the APSPushNotifications and APSCloudPush APIs. To run the sample application, you'll first need to create an Android application in Dashboard (or use an existing application) and configure its push notification settings to include a GCM or FCM sender ID and application key. APSCloudPush requires Google Play services, so you'll also need to add that library as a dependency to your project.

To import and run the APSCloudPushExample application:

  1. In Eclipse, select File > Import > General > Existing Projects into Workspace and click Browse.
  2. Navigate to the appcelerator-sdk-android-<VERSION>/examples/APSCloudPushExample folder and click Open.
  3. Click Finish to import the project.
  4. Add the Google Play services to your project (see Android Project Requirements for instructions).
  5. In, locate the following line and replace << YOUR APP KEY >> with the application key generated by Dashboard:

  6. Run the application on an Android device or emulator.