Updated on 2024-11-06 GMT+08:00

Android SDK Access Example

This topic describes how to use AMQP to connect the Android system to the IoT platform and receive subscribed messages from the platform.

Preparations

  • Install Android Studio. Go to the Android Studio website to download and install a desired version. The following uses Android Studio 4.1.1 running on 64-bit Windows as an example.
    Figure 1 Downloading Android Studio
  • Install the JDK. You can also use the built-in JDK of the IDE.
    1. Go to the Oracle website to download a desired version. The following uses JDK 8 for Windows x64 as an example.
    2. After the download is complete, run the installation file and install Node.js as prompted.

Development Environment

The development environments used in this example are as follows:

Sample Code

  1. Download the AMQP demo.
  2. Run Android Studio, click Open, and select the sample code downloaded in 1.

    Figure 2 Incorporate into existing projects

  3. Import the sample code.

    Figure 3 Importing the project structure

  4. (Optional) Set AMQP connection parameters in the res\values\strings.xml file in advance. For details, see AMQP Client Access.

    Figure 4 Modifying connection parameters

  5. In the AVD Manager configuration, select a device model and a virtual device of API level 28 for debugging.

    Figure 5 Configuring the AVD Manager

  6. Start the demo for debugging.

    Figure 6 Starting the demo

Related Information

AMQP connection configuration page is displayed in the following figure. INSTANCE ID is mandatory when multiple standard IoTDA instances are purchased in the same region. For details about the parameters, see AMQP Client Access.
Figure 7 AMQP connection configuration
  • The following figure shows the page indicating that the connection is successful after the parameter modification.
    Figure 8 Connection succeeded
  • The following figure shows the page indicating that the transfer data is successfully obtained.
    Figure 9 Receiving platform messages
  • The following figure shows the page indicating that the network connection failed.
    Figure 10 Network connection failed
  • The following figure shows the page indicating that access information (accessKey, accessCode, and instanceId) is incorrect.
    Figure 11 Incorrect access information
  • The following figure shows the page indicating that the queue does not exist.
    Figure 12 Queue not existing