Exploring CMS DEMO Project: Map, Location and Ads Kit Samples in Android

Emine İNAN
Huawei Developers
Published in
6 min readAug 22, 2024
Photo by Solen Feyissa on Unsplash

Introduction🌌

Common Mobile Services (CMS) simplifies the integration of Huawei and Google Mobile Services through a single solution, and the CMS DEMO project offers practical examples to help developers easily incorporate the CMS library into their projects.

In this article, we will explore the CMS DEMO, focusing on Map, Location, and Ads Kit Samples in Android.

You can check out this article for more detailed information about the CMS library:

A Quick Overview of the CMS DEMO Project

To facilitate smooth integration, the CMS DEMO offers a live demo that showcases the capabilities and functionalities of the Common Mobile Services library. This demo application is continually updated with new kit demos, ensuring developers can access the latest features and improvements.

In the following section, we will explore the CMS DEMO in detail, focusing on practical examples of Map, Location, and Ads Kit samples in Android.

Map and Location Kits: Integrating and Displaying Location Data on Maps

In this section, we’ll explore how to integrate Map Kit and Location Kit into your Android application. With the example taken from the CMS DEMO project, we will learn how to set up the environment, retrieve the user’s current location, and display it on a map.

MapScreen

🚩 Step 1: Creating the XML Layout for the MapScreen

First, create a fragment file for the MapScreen and define its layout in the XML file.

Use ConstraintLayout to manage the positions of the UI elements, placing a TextView at the top to display the map screen title and a CommonMapView below it to display the map initialized with an API key.

fragment_map_screen.xml

🚩 Step 2: Implementing the MapScreen Fragment

Open MapScreen fragment to implement to manage map and location data.

💠 onCreateView()
This method is responsible for initializing the fragment’s view. It inflates the layout using FragmentMapScreenBinding initializes the mapView and calls showAlertDialog() and initLocation() .

MapScreen.kt

💠 showAlertDialog()
This function presents an alert dialog to inform the user about the Map and Location Kit services being utilized. It retrieves the service information from a predefined list and displays it.

MapScreen.kt

💠 initLocation()
This function initializes the LocationClient, which is essential for tracking the user's location. It also enables GPS if it isn't already on, and if successful, it requests location updates by calling requestLocationUpdates().

MapScreen.kt

💠 requestLocationUpdates()
This method requests continuous location updates from the LocationClient. Depending on the success or failure of the request, it provides appropriate feedback to the user. If successful, it proceeds to retrieve the last known location.

MapScreen.kt

💠 getLastLocation()
This function retrieves the user’s last known location. If successful, it initializes the map with the user’s current latitude and longitude, displaying their location on the map.

MapScreen.kt

💠 initMap()
Once the user’s location is acquired, initMap() is called to set up the map view. It adds a marker at the user's location and animates the camera to focus on that point.

MapScreen.kt

💠 removeLocationUpdates()
Finally, this method stops location updates once they are no longer needed to conserve resources.

MapScreen.kt

Ads Kit: Integrating Ads in Your App

In this section, we’ll explore how to integrate Ads Kit into your Android application. With the example taken from the CMS DEMO project, we will learn how to set up the environment, load various ad formats such as banner, interstitial, and rewarded ads, and display them in the app.

AdsScreen
AdsScreen

🚩 Step 1: Creating the XML Layout for the AdsScreen

First, create a fragment file for the AdsScreen and define its layout in the XML file.

Use ConstraintLayout containing a CommonSplashAdView for splash ads, a CommonBannerAdView for banner ads, a ConstraintLayout with TextView and Button elements for various ad types, and a ProgressBar for loading indicators.

fragment_ads_screen.xml

🚩 Step 2: Implementing the AdsScreen Fragment

Open AdsScreen fragment to implement and load various ad formats.

💠 onCreateView()
This method is responsible for initializing the fragment’s view. It inflates the layout using FragmentAdsScreenBinding and calls showAlertDialog() and initUI().

AdsScreen.kt

💠 showAlertDialog()
This function presents an alert dialog to inform the user about the Ads Kit service being utilized. It retrieves the service information from a predefined list and displays it.

AdsScreen.kt

💠 initUI()
This function sets up click listeners for the buttons in the UI to trigger different types of ads. It binds each button’s click event to the corresponding ad display function.

AdsScreen.kt

💠 showSplashAd()
This function displays a splash ad by showing a progress indicator, loading the splash ad, and handling the ad load callbacks. It shows a toast message about the ad loading status and simulates ad dismissal after it is displayed.

AdsScreen.kt

💠 showBannnerAd()
This function displays a banner ad by initializing the banner ad view, showing a toast message about the ad loading status, and handling the ad load callbacks to notify about success or failure.

AdsScreen.kt

💠 showRewardedAd()
This function displays a rewarded ad by showing a progress indicator, loading the rewarded ad, and handling the ad load callbacks. It shows a toast message about the ad loading status, rewards the user upon ad completion, and displays appropriate messages based on the ad’s outcome.

AdsScreen.kt

💠 showInterstitialAd()
This function displays an interstitial ad by showing a progress indicator, loading the interstitial ad, and handling the ad load callbacks. It shows a toast message about the ad loading status and displays the ad upon successful load, hiding the progress indicator afterward.

AdsScreen.kt

💠 simulateAdDismissal()
This function simulates the dismissal of an ad by using a Handler to post a delayed task. It restores the visibility of the main content view after a 3-second delay.

AdsScreen.kt

💠 showProgress()
This function shows the progress indicator and hides the main content view to indicate that an ad is being loaded.

AdsScreen.kt

💠 hideProgress()
This function hides the progress indicator and shows the main content view, indicating that the ad has finished loading or failed.

AdsScreen.kt

Conclusion🤠

In this article, we’ve explored the integration of Map, Location, and Ads Kit samples in an Android application using the CMS DEMO project. By following these examples, you can effectively implement these services in your projects, enhancing functionality and user experience. Feel free to explore the CMS DEMO further and share your feedback on how it can benefit your projects.

Feeling The End GIF

--

--

Emine İNAN
Huawei Developers

Android Developer by day, Android Developer by night. @Huawei