Replace {version} with the actual SDK version number.
For example:
implementation 'com.huawei.hms:location:4.0.2.300'
Currently, HUAWEI Location Kit can only be used on Huawei phones.
To use HUAWEI Location Kit, you need to add the following dependencies:
- dependencies {
- implementation 'com.huawei.hms:location:{version}'
- }
Replace {version} with the actual SDK version number.
For example:
implementation 'com.huawei.hms:location:4.0.2.300'
If you have integrated other services of the HMS Core, you are advised to use the HMS SDK of the same version.
1. Ensure that you have installed HMS Core (APK) 3.0.0.300 or a later version.
2. Verify that the location permission is granted to the HMS Core (APK). Choose Settings -> Apps -> Permissions -> Location (The menu may vary depending on the OS version.), and ensure that Location is enabled for HMS Core. On a device running EMUI 10.0 or higher version, Location must be set to Always for HMS Core.
3. Ensure that Location is enabled for the app to be tested.
4. Ensure that Location is enabled on the notification panel.
5. Verify that the GPS or network signal is strong. In regions where the GPS or network signal is weak, the location information may fail to be obtained.
6. Verify that a SIM card is inserted on the phone. It is recommended that an SIM card be inserted on the phone so that HUAWEI Location Kit can provide more precise locations.
The getLastLocation API obtains the location information from the system cache, which may fail sometimes. If the obtained location information is null, the possible cause is that the system cache has been cleared. In this case, the requestLocationupdates API needs to be called to obtain the location information and update the cache, and then the getLastLocation API can be called to obtain the location information.
After isMockMode is set to true using setMockMode(boolean isMockMode), the HUAWEI Location Kit service will return the location set by setMockLocation(Location mockLocation) instead of using the GPS or network location.
Note: Once the location mock mode is enabled, the location function of all apps using the HUAWEI Location Kit service on the device will be affected. Therefore, when the location mock mode is not required anymore, you are advised to set isMockMode to false using setMockMode(boolean isMockMode).
You can check whether the current location is a mock location based on the return valueof location.getExtra().getBoolean("mockLocation"). If the return value is true, the current location is a mock location. If the return value is false, the current location is a real location.
When a HUAWEI Location Kit API fails to be called, you can preliminarily check the causes based on the return value.
Obtain the Task object returned by the API, call addOnFailureListener(), override the onFailure(Exception e) method, and execute e.getMessage to print the error information. The error information contains the error code and error description. For details about the error codes, please refer to Common Result Codes.
HMS Core 4.0.2 supports some models of EMUI 8.0 and later. HMS Core 4.0.4 fixes some compatibility bugs. You are advised to update the version to 4.0.4 or later.
The preceding table does not describe the support of the simulator.
[Q8] Are there any restrictions on the environment for using the geofence function? Can the geofence function be used both indoor and outdoor?
Currently, geofences are high-precision ones and use the GPS or cell to obtain the location. Therefore, it is recommended that the geofence function be used outdoor to achieve higher precision.
[Q9] What is the recommended radius for a geofence?
It is recommended that the geofence radius be greater than or equal to 200 meters. Precision cannot be assured if the geofence radius is less than 200 meters.
[Q10] Is there a limit on the number of geofences?
A maximum of 100 geofences can be added for each app.
[Q11] What phone models and EMUI versions support the activity recognition function?
EMUI versions: EMUI 9.1.1 and later.
Phone models: Models equipped with Hisilicon Kirin 960 or later.
The preceding table does not describe the support of the simulator.
[Q12] How can I improve the activity recognition success rate?
The IN_VEHICLE and ON_BICYCLE modes of activity recognition use the cell information. During function testing, you are advised to insert an SIM card to the phone and connect to the 5G, 4G, 3G, or 2G network.
[Q13] How can I capture logs?
If the location information fails to be obtained after you call the HUAWEI Location Kit service API and the fault is hard to locate, you can capture run logs and contact Huawei technical support.
The procedure for capturing logs is as follows:
1. Run the following command to connect to the phone:
adb logcat –v time > D:\hwlocation.log
2. Reproduce the fault scenario.
3. Press Ctrl+C.
In addition, record the device model, EMUI version, HMS Core version, integrated SDK version, and fault reproduction procedure so that technical personnel can quickly locate the fault.
minSdkVersion | Earliest Android Version | Earliest EMUI Version |
| 19 | 4.4 | 3.0 |
During locating, the third-party app triggers the location service of HMS Core (APK). To ensure normal locating, the third-party app user needs to assign the location permission to HMS Core (APK), which is irrelevant to the third-party app. For an Android Q device, the user needs to assign HMS Core (APK) with the permission to (always) obtain the device location in the background.
The setMockMode(boolean isMockMode) method can only be used on Huawei phones running EMUI 5.0 or later.
If the passed parameter is Context instead of Activity when LocationService and ActivityIdentification are used to create a Client instance, no update wizard is displayed and only error code 907135003 is returned. In this case, you need to manually update HMS Core (APK). To show the update wizard, you are advised to pass the Activity parameter instead of the Context parameter to create a Client instance.
Currently, high-precision locations can only be obtained on P40-series phones in the Chinese mainland.