[Huawei P30 Pro Themes, Apps, and Mods] XDARoni:
Introduction

This article is based on Multiple HMS services application. I have created Hotel Booking application using HMS Kits. We need mobile app for reservation hotels when we are traveling from one place to another place.

In this article, I am going to implement HMS Location Kit & Shared Preferences.

View attachment 5230279

Flutter setup

Refer this URL to setup Flutter.

Software Requirements

1. Android Studio 3.X

2. JDK 1.8 and later

3. SDK Platform 19 and later

4. Gradle 4.6 and later

Steps to integrate service

1. We need to register as a developer account in AppGallery Connect.

2. Create an app by referring to Creating a Project and Creating an App in the Project

3. Set the data storage location based on current location.

4. Enabling Required Services: Location Kit.

5. Generating a Signing Certificate Fingerprint.

6. Configuring the Signing Certificate Fingerprint.

7. Get your agconnect-services.json file to the app root directory.

Important: While adding app, the package name you enter should be the same as your Flutter project’s package name.

Note: Before you download agconnect-services.json file, make sure the required kits are enabled.

Development Process

Create Application in Android Studio.

1. Create Flutter project.

2. App level gradle dependencies. Choose inside project Android > app > build.gradle.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

Root level gradle dependencies

maven {url 'https://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.4.1.300'

Add the below permissions in Android Manifest file.

<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION" />
<application ...
</manifest>

3. Refer below URL for cross-platform plugins. Download required plugins.

https://developer.huawei.com/consum...y-V1/flutter-sdk-download-0000001050304074-V1

4. After completing all the steps above, you need to add the required kits’ Flutter plugins as dependencies to pubspec.yaml file. You can find all the plugins in pub.dev with the latest versions.

dependencies:
flutter:
sdk: flutter
shared_preferences: ^0.5.12+4
bottom_navy_bar: ^5.6.0
cupertino_icons: ^1.0.0
provider: ^4.3.3

huawei_location:
path: ../huawei_location/

flutter:
uses-material-design: true
assets:
- assets/images/

5. After adding them, run flutter pub get command. Now all the plugins are ready to use.

6. Open main.dart file to create UI and business logics.

Location kit

HUAWEI Location Kit assists developers in enabling their apps to get quick and accurate user locations and expand global positioning capabilities using GPS, Wi-Fi, and base station locations.

Fused location: Provides a set of simple and easy-to-use APIs for you to quickly obtain the device location based on the GPS, Wi-Fi, and base station location data.

Activity identification: Identifies user motion status through the acceleration sensor, cellular network information, and magnetometer, helping you adjust your app based on user behaviour.

Geofence: Allows you to set an interested area through an API so that your app can receive a notification when a specified action (such as leaving, entering, or lingering in the area) occurs.

Integration

Permissions

First of all we need permissions to access location and physical data.

Create a PermissionHandler instance,add initState() for initialize.

final PermissionHandler permissionHandler;
@override
void initState() {
permissionHandler = PermissionHandler(); super.initState();
}

Check Permissions

We need to check device has permission or not using hasLocationPermission() method.

void hasPermission() async {
try {
final bool status = await permissionHandler.hasLocationPermission();
if(status == true){
showToast("Has permission: $status");
}else{
requestPermission();
}
} on PlatformException catch (e) {
showToast(e.toString());
}
}

If device don’t have permission,then request for Permission to use requestLocationPermission() method.

void requestPermission() async {
try {
final bool status = await permissionHandler.requestLocationPermission();
showToast("Is permission granted");
} on PlatformException catch (e) {
showToast(e.toString());
}
}

Fused Location

Create FusedLocationPrvoiderClient instance using the init() method and use the instance to call location APIs.

final FusedLocationProviderClient locationService

@override
void initState() {
locationService = FusedLocationProviderClient(); super.initState();
}

getLastLocation()

void getLastLocation() async {
try {
Location location = await locationService.getLastLocation();
setState(() {
lastlocation = location.toString();
print("print: " + lastlocation);
});
} catch (e) {
setState(() {
print("error: " + e.toString());
});
}
}

getLastLocationWithAddress()

Create LocationRequest instance and set required parameters.

final LocationRequest locationRequest;
locationRequest = LocationRequest()
..needAddress = true
..interval = 5000;

void _getLastLocationWithAddress() async {
try {
HWLocation location =
await locationService.getLastLocationWithAddress(locationRequest);
setState(() {
String street = location.street;
String city = location.city;
String countryname = location.countryName;
currentAddress = '$street' + ',' + '$city' + ' , ' + '$countryname';
print("res: $location");
});
showToast(currentAddress);
} on PlatformException catch (e) {
showToast(e.toString());
}
}

Location Update using Call back

Create LocationCallback instance and create callback functions in initstate().

LocationCallback locationCallback;
@override
void initState() {
locationCallback = LocationCallback(
onLocationResult: _onCallbackResult,
onLocationAvailability: _onCallbackResult,
);
super.initState();
}

void requestLocationUpdatesCallback() async {
if (_callbackId == null) {
try {
final int callbackId = await locationService.requestLocationUpdatesExCb(
locationRequest, locationCallback);
_callbackId = callbackId;
} on PlatformException catch (e) {
showToast(e.toString());
}
} else {
showToast("Already requested location updates.");
}
}

void onCallbackResult(result) {
print(result.toString());
showToast(result.toString());
}

I have created Helper class to store user login information in locally using shared Preferences class.

class StorageUtil {
static StorageUtil _storageUtil;
static SharedPreferences _preferences;

static Future<StorageUtil> getInstance() async {
if (_storageUtil == null) {
var secureStorage = StorageUtil._();
await secureStorage._init();
_storageUtil = secureStorage;
}
return _storageUtil;
}

StorageUtil._();

Future _init() async {
_preferences = await SharedPreferences.getInstance();
}

// get string
static String getString(String key) {
if (_preferences == null) return null;
String result = _preferences.getString(key) ?? null;
print('result,$result');
return result;
}

// put string
static Future<void> putString(String key, String value) {
if (_preferences == null) return null;
print('result $value');
return _preferences.setString(key, value);
}
}

Result

View attachment 5230281

Tips & Tricks

1. Download latest HMS Flutter plugin.

2. To work with mock location we need to add permissions in Manifest.XML.

3. Whenever you updated plugins, click on pug get.

Conclusion

We implemented simple hotel booking application using Location kit in this article. We have learned how to get Lastlocation, getLocationWithAddress and how to use callback method, in flutter how to store data into Shared Preferences in applications.

Thank you for reading and if you have enjoyed this article, I would suggest you to implement this and provide your experience.

Reference

Location Kit URL

Shared Preferences URL

Read full article