Service Introduction

注意
  1. To use the high-precision location capability, you need to comply with relevant laws and regulations. In addition, you and Huawei need to apply to related departments in China together for the license for using the high-precision location deflection plugin.
  2. Currently, the high-precision location capability is available only in Shenzhen, Guangzhou, Suzhou, Hangzhou, Chongqing, Chengdu, Tianjin, and Dongguan in the Chinese mainland. More cities will be supported in the future.

The Location SDK for Android combines the Global Navigation Satellite System (GNSS), Wi-Fi, and base station location functionalities into your app to build up global positioning capabilities, allowing you to provide flexible location-based services for global users. Such capabilities mainly include fused location, activity identification, geofence, high-precision location, and indoor location. You can call one or more of these capabilities as needed.

You can click here to watch the MOOC video.

Function Experience

Fused Location

LocationCallback mLocationCallback = null;
LocationRequest mLocationRequest;
FusedLocationProviderClient mFusedLocationProviderClient;
// Obtain a FusedLocationProviderClient instance.
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
mLocationRequest = new LocationRequest();
// Set the interval for requesting location updates to 6000 ms. The default value is 5000 ms.
mLocationRequest.setInterval(6000);
// Set the location type. PRIORITY_HIGH_ACCURACY indicates fused location.
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the number of location updates to 100. The default value is Integer.MAX_VALUE.
// Special case: If only one location update needs to be requested, set the number of location updates to 1. In this case, the location update is requested once only and you do not need to stop requesting location updates after the update is complete.
mLocationRequest.setNumUpdates(100);
// Set the coordinate type for the returned location. COORDINATE_TYPE_WGS84 indicates the WGS84 coordinate type, and COORDINATE_TYPE_GCJ02 indicates the GCJ02 coordinate type. The default value is COORDINATE_TYPE_WGS84. If the coordinate type is set to GCJ02, the location information after coordinate conversion needs to be obtained from the HWLocation object.
mLocationRequest.setCoordinateType(LocationRequest.COORDINATE_TYPE_WGS84);
if (null == mLocationCallback) {
    // Set the location callback.
    mLocationCallback = new LocationCallback() {
        @Override        
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult != null) {                
                List<Location> locations = locationResult.getLocations();
                // TODO: Process the obtained location.
            }        
        }    
    };
}
// Initiate location.
mFusedLocationProviderClient 
    .requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper());

Activity Identification

ActivityIdentificationService activityIdentificationService;
PendingIntent pendingIntent;
// Obtain an activityIdentificationService instance.
activityIdentificationService = ActivityIdentification.getService(this);
pendingIntent = getPendingIntent();   
// Request activity identification updates. 
// The first parameter indicates the interval (in milliseconds) for requesting activity identification updates. Currently, this parameter is set to 5000. The second parameter is pendingIntent, which is used to associate a broadcast receiver.
activityIdentificationService.createActivityIdentificationUpdates(5000, pendingIntent)
    // Define callback for task success.
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            // TODO: Processing when the task is successful.
        }
     })
    // Define callback for task failure.
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            // TODO: Processing when the task fails to be executed.
        }
    });
// Dynamically register a broadcast receiver through PendingIntent to detect activity identification updates.
private PendingIntent getPendingIntent() {
    Intent intent = new Intent(this, LocationBroadcastReceiver.class);
    intent.setAction(LocationBroadcastReceiver.ACTION_PROCESS_LOCATION);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    } else {
        // For devices running Android 12 or later, you need to configure the mutability of pendingIntent. By default, the configuration is PendingIntent.FLAG_MUTABLE.
        // If compileSdkVersion is 30 or earlier, you can replace PendingIntent.FLAG_MUTABLE with 1<<25.
        // PendingIntent.FLAG_MUTABLE can be used only when buildToolsVersion is 31 or later.
        return PendingIntent.getBroadcast(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
    }
}

Geofence

GeofenceService geofenceService = LocationServices.getGeofenceService(this);
ArrayList<Geofence> geofenceList;
PendingIntent pendingIntent;
geofenceList = new ArrayList<Geofence>();
geofenceList.add(new Geofence.Builder().setUniqueId("mGeofence")    
    .setValidContinueTime(10000)
    // Pass the longitude and latitude of the geofence center, and radius of the circle geofence (unit: meters).
    .setRoundArea(48.8556753545395, 2.2928132868838977, 1000) 
    // Pass a value to specify status to listen for. The value 7 indicates to listen for the entering, exiting, and staying states.
    .setConversions(7)
    // Set the stay duration.
    .setDwellDelayTime(1000 * 10).build());
GeofenceRequest.Builder builder = new GeofenceRequest.Builder();
// Trigger a callback immediately after the geofence is added, if the user is already within the geofence.
builder.setInitConversions(7);
builder.createGeofenceList(geofenceList);
GeofenceRequest geofenceRequest = builder.build();
pendingIntent = getPendingIntent();
// Add a geofence by passing PendingIntent, and process the response to the geofence addition request.
geofenceService.createGeofenceList(geofenceRequest, pendingIntent)  
    .addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override        
        public void onComplete(Task<Void> task) {
            if (task.isSuccessful()) {
                // TODO: The task is successful.
           } else {
                // TODO: The task fails.
            }
       }
});
private PendingIntent getPendingIntent() {
    Intent intent = new Intent(this, GeoFenceReceiver.class);
    intent.setAction(GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    } else {
        // For devices running Android 12 or later, you need to configure the mutability of pendingIntent. By default, the configuration is PendingIntent.FLAG_MUTABLE.
        // If compileSdkVersion is 30 or earlier, you can replace PendingIntent.FLAG_MUTABLE with 1<<25.
        // PendingIntent.FLAG_MUTABLE can be used only when buildToolsVersion is 31 or later.
        return PendingIntent.getBroadcast(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
    }
}

High-Precision Location

FusedLocationProviderClient fusedLocationProviderClient;
LocationCallback mLocationHDCallback = null;
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// Request high-precision location. 
LocationRequest locationRequest = new LocationRequest();
// Set the location type. PRIORITY_HD_ACCURACY indicates high-precision location.
locationRequest.setPriority(LocationRequest.PRIORITY_HD_ACCURACY);
// Set the coordinate type for the returned location. COORDINATE_TYPE_WGS84 indicates the WGS84 coordinate type, and COORDINATE_TYPE_GCJ02 indicates the GCJ02 coordinate type. The default value is COORDINATE_TYPE_WGS84. If the coordinate type is set to GCJ02, the location information after coordinate conversion needs to be obtained from the HWLocation object.
// If the returned location is a high-precision location, its coordinate type will not be converted.
locationRequest.setCoordinateType(LocationRequest.COORDINATE_TYPE_WGS84);
if (null == mLocationHDCallback) {
    // Location request callback.
    mLocationHDCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationRequest){
            // TODO: The location callback is successful.
        }
    };
}
// Initiate high-precision location.
fusedLocationProviderClient.requestLocationUpdatesEx(locationRequest, mLocationHDCallback, Looper.getMainLooper())
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            // TODO: Processing when the task is successful.
        }
     })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            // TODO: Processing when the task fails to be executed.
        }
    });

Indoor Location

FusedLocationProviderClient fusedLocationProviderClient;
LocationCallback mLocationIndoorHDCallback = null;
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// Obtain an indoor location request.
LocationRequest locationRequest = new LocationRequest();
// Set the location type. PRIORITY_INDOOR indicates indoor location.
locationRequest.setPriority(LocationRequest.PRIORITY_INDOOR);
// Set the coordinate type for the returned location. COORDINATE_TYPE_WGS84 indicates the WGS84 coordinate type, and COORDINATE_TYPE_GCJ02 indicates the GCJ02 coordinate type. The default value is COORDINATE_TYPE_WGS84. If the coordinate type is set to GCJ02, the location information after coordinate conversion needs to be obtained from the HWLocation object.
locationRequest.setCoordinateType(LocationRequest.COORDINATE_TYPE_WGS84);
if (null == mLocationIndoorHDCallback) {
    // Location request callback.
    mLocationIndoorHDCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult){
            // TODO: The location callback is successful.
    
        }
    };
}
// Initiate indoor location.
fusedLocationProviderClient
    .requestLocationUpdatesEx(locationRequest, mLocationIndoorHDCallback, Looper.getMainLooper())
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            // TODO: Processing when the task is successful.
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            // TODO: Processing when the task fails to be executed.
        }
    });

Function Restrictions

Function

Chinese Mainland

Outside the Chinese Mainland

Remarks

High-precision location

Supported on some models

Not supported

Currently, this function is available only in Shenzhen, Guangzhou, Suzhou, Hangzhou, Chongqing, Chengdu, Tianjin, and Dongguan in the Chinese mainland. More cities will be supported in the future.

It is now supported on the following models: Mate X2, Mate 40, Mate 40E, Mate 40 Pro, Mate 40 Pro+, P50 Pro (Kirin 9000), P40, P40 Pro, P40 Pro+, nova 8 Pro, Honor 30 Pro, and Honor 30 Pro+ (More models will be supported in the future.)

Indoor location

Supported on some models

Not supported

Currently, this function is available only in some shopping malls in the Chinese mainland. For details, please refer to Supported Buildings.

Network location

Supported

Supported

This function is unavailable in the Chinese mainland. Outside the Chinese mainland, this function is disabled by default and needs to be manually enabled by users.

Other functions

Supported

Supported

Other functions are the same both inside and outside the Chinese mainland.

搜索
请输入您想要搜索的关键词