Beginner: Integration of Huawei Kits (Account Kit, Ads Kit and Site Kit) to build Food app in Android (Kotlin)
Overview
In this article, we can learn how to integrate Account Kit, Ads Kit and Site kit in food applications. Account Kit guides you to login through the Huawei sign in button. Ads Kit will advertise on the application. Site Kit guides you to select the places or locations. Mobile apps make our world better and easier customer to prefer comfort and quality instead of quantity.
This article will guide you to show favourite hotels or nearby hotels.
Prerequisites
1. Must have a Huawei Developer Account.
2. Must have a Huawei phone with HMS 4.0.0.300 or later.
3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.
Integration Preparations
1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.
2. Create a project in android studio, refer Creating an Android Studio Project.
3. Generate a SHA-256 certificate fingerprint.
4. To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > app > Tasks > android, and then click signingReport, as follows.
Note: Project Name depends on the user created name.
5. Create an App in AppGallery Connect.
6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.
7. Enter SHA-256 certificate fingerprint and click tick icon as follows.
Note: Above steps from Step 1 to 7 is common for all Huawei Kits.
8. Click Manage APIs tab and Enable required kits (Account Kit and Site Kit).
9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.
10. Add the below plugin and dependencies in build.gradle(Module) file.
11. Now Sync the gradle.
12. Add the below permissions in AndroidManifest.xml file.
Account Kit Overview
Huawei Account Kit provides for developers with simple, secure, and quick sign-in and authorization functions. User is not required to enter accounts, passwords and waiting for authorization. User can click on Sign in with Huawei ID button to quickly and securely sign in to the app. We can implement authorization code sign in use case to login to application.
Signing with Authorization Code
In this method, Account kit allows to sign-in using an ID in authorization code mode. When you click the Huawei ID signing in button, it requires the AccountAuthParams and create a service with authParams, then add startActivityForResult() method in Huawei ID signing in button with service and requestCode.
Find the below code to get this method.
When the user clicks login with Huawei ID button, the app needs to authorize and login operations from the user.
Find the below code to get the result.
Add the below code in activity_main.xml
Result
Sign Out
Use this service to sign out the user from application.
Find the below code.
Result
Ads Kits Overview
Huawei Ads provides to developers a wide-ranging capabilities to deliver good quality ads content to users. This is the best way to reach target audience easily and can measure user productivity. It is very useful when we publish a free app and want to earn some money from it.
HMS Ads Kit has 7 types of Ads kits. Now we can implement Banner Ads in this application.
Banner Ads are rectangular ad images located at the top, middle or bottom of an application’s layout. Banner ads are automatically refreshed at intervals. When a user clicks a banner ad, in most cases the user will guide to the advertiser’s page.
Standard Banner Ad Dimensions
The following table lists the standard banner ad dimensions.
In this application, we can display Banner Ads in the login page, find the below code.
Add BannerView in activity_main.xml.
Result
Site Kit Overview
Site Kit provides the place related services for apps. It provides that to search places with keywords, find nearby place, place suggestion for user search, and find the place details using the unique id.
Huawei Site Kit can be used in any industry based on the requirements, such as Hotels/Restaurants, Ecommerce, Weather Apps, Tours and Travel, Hospitality, Health Care etc.
Features
- Keyword Search: Converts co-ordinates into street address and vice versa.
- Nearby Place Search: Searches for nearby places based on the current location of the user’s device.
- Place Detail Search: Searches for details about a place as reviews, time zone etc.
- Place Search Suggestion: Suggest place names and addresses.
Steps
1. Create TextSearchRequest object, which is used as the request body search by Keyword.
TextSearchRequest Parameters.
a. Mandatory
Query: search keyword. Which is entered by user from the application.
b. Optional
- location: longitude and latitude to which search results need to be biased.
- radius: search radius, in meters. The value ranges from 1 to 50000. The default value is 50000.
- bounds: coordinate bounds to which search results need to be biased.
- poiTypes: list of POI(Point of Interest) types.
- countryCode: country code, which complies with the ISO 3166–1 alpha-2 standards. This parameter is used to restrict search results to the specified country.
- language: language in which search results are returned. For details about the value range, please refer to language codes in Language Mapping. If this parameter is not passed, the language of the query field (preferred) or the local language is used.
- politicalView: Political view parameter. The value is a two-digit country code specified in the ISO 3166–1- alpha-2 standard.
- pageSize: number of records on each page. The value ranges from 1 to 20. The default value is 20.
- pageIndex: number of the current page. The value ranges from 1 to 60. The default value is 1.
2. Create a SearchResultListener object to listen for the search result.
3. Now call the textSearch() to get the result.
Result
Final Code
Add the below code in MainActivity.kt.
Add the below code in Home.kt.
Tips and Tricks
- Make sure you are already registered as Huawei developer.
- Enable Account kit and Site kit service in the App Gallery.
- Make sure your HMS Core is latest version.
- Make sure you have added the agconnect-services.json file to app folder.
- Make sure you have added SHA-256 fingerprint without fail.
- Make sure all the dependencies are added properly.
- Banner ads be can also added programmatically.
Conclusion
In this article, we have learnt integration of Account Kit, Ads Kit and Site Kit in food applications. It will guide you to show favourite hotels or nearby hotels based on the user selection.
I hope you have read this article. If you found it is helpful, please provide likes and comments.
References
For more details, you can visit to HMS Core GitHub organization (https://github.com/HMS-Core) to download demos and sample codes.