Currently, the Google APIs in the GoogleApiClient class have been deprecated. In the Add HMS API and To HMS API scenarios, you need to manually convert the APIs. The following content helps you manually convert the deprecated APIs to the latest implementations.
Google API | Add HMS API | To HMS API |
|---|---|---|
new DetectedActivity(activityType, confidence) | ActivityIdentificationData(identificationActivity, possibility) Note: If the method throws a new exception specified by ApiException, ActivityIdentificationData fails to be constructed. In this case, you need to handle this issue by yourself. | new ActivityIdentificationData(identificationActivity, possibility) Note: If the method throws a new exception specified by ApiException, ActivityIdentificationData fails to be constructed. In this case, you need to handle this issue by yourself. |
The implementation of DetectedActivity() is as follows:
import com.google.android.gms.location.DetectedActivity;
import java.util.ArrayList;
import java.util.HashMap;
HashMap<Integer, Integer> detectedActivitiesMap = new HashMap<>();
for (DetectedActivity activity : detectedActivities) {
detectedActivitiesMap.put(activity.getType(), activity.getConfidence());
}
// Every time we detect new activities, we want to reset the confidence level of ALL
// activities that we monitor. Since we cannot directly change the confidence
// of a DetectedActivity, we use a temporary list of DetectedActivity objects. If an
// activity was freshly detected, we use its confidence level. Otherwise, we set the
// confidence level to zero.
ArrayList<DetectedActivity> tempList = new ArrayList<>();
for (int i = 0; i < Constants.MONITORED_ACTIVITIES.length; i++) {
int confidence = detectedActivitiesMap.containsKey(Constants.MONITORED_ACTIVITIES[i])
? detectedActivitiesMap.get(Constants.MONITORED_ACTIVITIES[i]) : 0;
tempList.add(new DetectedActivity(Constants.MONITORED_ACTIVITIES[i], confidence));
}
// Remove all items.
clear();
// Adding the new list items notifies attached observers that the underlying data has
// changed and views reflecting the data should refresh.
for (DetectedActivity detectedActivity : tempList) {
add(detectedActivity);
} In the Add HMS API scenario, the following implementation is recommended:
// Pay attention to the name of the package to be imported.
import com.huawei.hms.common.ApiException;
import com.huawei.hms.location.ActivityIdentificationData;
import java.util.ArrayList;
import java.util.HashMap;
HashMap<Integer, Integer> detectedActivitiesMap = new HashMap<>();
for (ActivityIdentificationData activity : detectedActivities) {
detectedActivitiesMap.put(activity.getIdentificationActivity(), activity.getPossibility());
}
// Every time we detect new activities, we want to reset the confidence level of ALL
// activities that we monitor. Since we cannot directly change the confidence
// of a DetectedActivity, we use a temporary list of DetectedActivity objects. If an
// activity was freshly detected, we use its confidence level. Otherwise, we set the
// confidence level to zero.
ArrayList<ActivityIdentificationData> tempList = new ArrayList<>();
for (int i = 0; i < Constants.MONITORED_ACTIVITIES.length; i++) {
int confidence = detectedActivitiesMap.containsKey(Constants.MONITORED_ACTIVITIES[i])
? detectedActivitiesMap.get(Constants.MONITORED_ACTIVITIES[i]) : 0;
try {
tempList.add(new ActivityIdentificationData(Constants.MONITORED_ACTIVITIES[i], confidence));
} catch (ApiException e) {
e.printStackTrace();
}
}
// Remove all items.
clear();
// Adding the new list items notifies attached observers that the underlying data has
// changed and views reflecting the data should refresh.
for (ActivityIdentificationData detectedActivity : tempList) {
add(detectedActivity);
} The Google APIs and constants in ActivityRecognition have been removed. You are advised to use the latest implementation functions to build your project.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
ActivityRecognition.API | Not provided | Not provided |
ActivityRecognition.ActivityRecognitionApi | Not provided | Not provided |
ActivityRecognition.removeActivityUpdates(GoogleApiClient client, PendingIntent callbackIntent) | ActivityRecognitionClient.removeActivityUpdates(PendingIntent callbackIntent) | ActivityIdentificationService.deleteActivityIdentificationUpdates(PendingIntent pendingIntent) |
ActivityRecognition.requestActivityUpdates(GoogleApiClient client, long detectionIntervalMillis, PendingIntent callbackIntent) | ActivityRecognitionClient.requestActivityUpdates(long detectionIntervalMillis, PendingIntent callbackIntent) | ActivityIdentificationService.createActivityIdentificationUpdates(long detectionIntervalMillis, PendingIntent callbackIntent) |
import com.google.android.gms.common.api.GoogleApiClient;
Intent intent = new Intent();
int requestCode = 1;
Context context = this;
long detectionIntervalMillis = 60 * 1000L;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Api<Api.ApiOptions.NoOptions> api = ActivityRecognition.API;
ActivityRecognitionApi activityRecognitionApi = ActivityRecognition.ActivityRecognitionApi;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
activityRecognitionApi.requestActivityUpdates(googleApiClient, detectionIntervalMillis, pendingIntent);
activityRecognitionApi.removeActivityUpdates(googleApiClient, pendingIntent); // Pay attention to the name of the package to be imported.
import org.xms.g.location.ActivityRecognition;
import org.xms.g.location.ActivityRecognitionClient;
import org.xms.g.tasks.OnCompleteListener;
import org.xms.g.tasks.Task;
int requestCode = 1;
Context context = this;
Intent intent = new Intent(context, DetectedActivityService.class);
long detectionIntervalMillis = 60 * 1000L;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognitionClient activityRecognitionClient = ActivityRecognition.getClient(context);
// Initiate a request.
Task<Void> requestTask =
activityRecognitionClient.requestActivityUpdates(detectionIntervalMillis, pendingIntent);
requestTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
});
// Remove a request.
Task<Void> removeTask = activityRecognitionClient.removeActivityUpdates(pendingIntent);
removeTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); DetectedActivityService.class:
import java.util.ArrayList;
import org.xms.g.location.ActivityRecognitionResult;
import org.xms.g.location.DetectedActivity;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class DetectedActivityService extends IntentService {
protected static final String TAG = "DetectedActivityService";
public DetectedActivityService() {
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
if (result != null) {
ArrayList<DetectedActivity> detectedActivities =
(ArrayList<DetectedActivity>) result.getProbableActivities();
for (DetectedActivity detectedActivity : detectedActivities) {
Log.i(TAG, detectedActivity.getType() + " " + detectedActivity.getConfidence() + "%");
}
}
}
} In the To HMS API scenario, the following implementation is recommended:
// Pay attention to the name of the package to be imported.
import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.location.ActivityIdentification;
import com.huawei.hms.location.ActivityIdentificationService;
Context context = this;
Intent intent = new Intent(context, DetectedService.class);
int requestCode = 1;
long detectionIntervalMillis = 60 * 1000L;
PendingIntent pendingIntent =
PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityIdentificationService activityRecognitionClient = ActivityIdentification.getService(context);
// Initiate a request.
Task<Void> createTask =
activityRecognitionClient.createActivityIdentificationUpdates(detectionIntervalMillis, pendingIntent);
createTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
});
// Remove a request.
Task<Void> deleteTask = activityRecognitionClient.deleteActivityIdentificationUpdates(pendingIntent);
deleteTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); DetectedService.class:
import java.util.ArrayList;
import com.huawei.hms.location.ActivityIdentificationData;
import com.huawei.hms.location.ActivityIdentificationResponse;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class DetectedService extends IntentService {
protected static final String TAG = "DetectedService";
public DetectedService() {
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
ActivityIdentificationResponse result = ActivityIdentificationResponse.getDataFromIntent(intent);
if (result != null) {
ArrayList<ActivityIdentificationData> detectedActivities =
(ArrayList<ActivityIdentificationData>) result.getActivityIdentificationDatas();
for (ActivityIdentificationData identificationData : detectedActivities) {
Log.i(TAG,
identificationData.getIdentificationActivity() + " " + identificationData.getPossibility() + "%");
}
}
}
} In the latest Google APIs, GeofencingApi has been deprecated. You are not advised to use the deprecated APIs. In the Add HMS API and To HMS API scenarios, you are advised to use the latest functions to process the corresponding implementations.
Deprecated APIs in GeofencingApi:
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
GeofencingApi.addGeofences(GoogleApiClient client, GeofencingRequest geofencingRequest, PendingIntent pendingIntent) GeofencingApi.addGeofences(GoogleApiClient client, List<Geofence> geofences, PendingIntent pendingIntent) | GeofencingClient.addGeofences(GeofencingRequest geofencingRequest, PendingIntent pendingIntent) | GeofenceService.createGeofenceList(GeofenceRequest geofenceRequest, PendingIntent pendingIntent) |
GeofencingApi.removeGeofences(GoogleApiClient client, List<String> geofenceRequestIds) | GeofencingClient.removeGeofences(List<String> geofenceRequestIds) | GeofenceService.deleteGeofenceList(List<String> geofenceRequestIds) |
GeofencingApi.removeGeofences(GoogleApiClient client, PendingIntent pendingIntent) | GeofencingClient.removeGeofences(PendingIntent pendingIntent) | GeofenceService.deleteGeofenceList(PendingIntent pendingIntent) |
Use the latest GeofencingClient class to add or remove a geofence to implement the geofencing function. The following describes the implementations in the Add HMS API and To HMS API scenarios.
The implementation in the Add HMS API scenario is as follows:
// Pay attention to the name of the package to be imported.
import org.xms.g.location.Geofence;
import org.xms.g.location.GeofencingClient;
import org.xms.g.location.GeofencingRequest;
import org.xms.g.location.LocationServices;
import org.xms.g.tasks.OnCompleteListener;
import org.xms.g.tasks.Task;
// Initialize request parameters.
String requestId = "request-ID";
double latitude = 33.666666D;
double longitude = 55.888888D;
float radius = 500.0f;
long expirationDuration = 1200000L;
int loiteringDelay = 20 * 1000;
int transitionTypes = GeofencingRequest.getINITIAL_TRIGGER_ENTER
| GeofencingRequest.getINITIAL_TRIGGER_DWELL | GeofencingRequest.getINITIAL_TRIGGER_EXIT;
int notificationResponsiveness = 30 * 1000;
// Add a fence to the fence list.
List<Geofence> geofences = new ArrayList<>();
Geofence.Builder builder = new Geofence.Builder();
builder.setRequestId(requestId);
builder.setCircularRegion(latitude, longitude, radius);
builder.setExpirationDuration(expirationDuration);
builder.setLoiteringDelay(loiteringDelay);
builder.setTransitionTypes(transitionTypes);
builder.setNotificationResponsiveness(notificationResponsiveness);
Geofence geofence = builder.build();
geofences.add(geofence);
// Build a request body.
GeofencingRequest.Builder geofenceRequestBuilder = new GeofencingRequest.Builder();
geofenceRequestBuilder.addGeofences(geofences);
geofenceRequestBuilder.setInitialTrigger(Geofence.getGEOFENCE_TRANSITION_ENTER
| Geofence.getGEOFENCE_TRANSITION_DWELL | Geofence.getGEOFENCE_TRANSITION_EXIT);
GeofencingRequest geofencingRequest = geofenceRequestBuilder.build();
// Create PendingIntent for the response.
Intent intent = new Intent();
int requestCode = 1;
Context context = this;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Initialize GeofencingClient.
GeofencingClient geofencingClient = LocationServices.getGeofencingClient(context); // Create a fence.
Task<Void> voidTask = geofencingClient.addGeofences(geofencingRequest, pendingIntent);
voidTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); // Delete a fence.
List<String> requestIds = new ArrayList<>();
requestIds.add(requestId);
Task<Void> removeTask = geofencingClient.removeGeofences(requestIds);
//Task<Void> removeTask = geofencingClient.removeGeofences(pendingIntent); // A fence can also be removed using this method.
removeTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); The implementation in the To HMS API scenario is as follows:
// Pay attention to the name of the package to be imported.
import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.location.Geofence;
import com.huawei.hms.location.GeofenceRequest;
import com.huawei.hms.location.GeofenceService;
import com.huawei.hms.location.LocationServices;
// Initialize request parameters.
String requestId = "request-ID";
double latitude = 33.666666D;
double longitude = 55.888888D;
float radius = 500.0f;
long expirationDuration = 1200000L;
int loiteringDelay = 20 * 1000;
int transitionTypes = GeofenceRequest.ENTER_INIT_CONVERSION | GeofenceRequest.EXIT_INIT_CONVERSION
| GeofenceRequest.DWELL_INIT_CONVERSION;
int notificationResponsiveness = 30 * 1000;
// Add a fence to the fence list.
List<Geofence> geofences = new ArrayList<>();
Geofence.Builder builder = new Geofence.Builder();
builder.setUniqueId(requestId);
builder.setRoundArea(latitude, longitude, radius);
builder.setValidContinueTime(expirationDuration);
builder.setDwellDelayTime(loiteringDelay);
builder.setConversions(transitionTypes);
builder.setNotificationInterval(notificationResponsiveness);
Geofence geofence = builder.build();
geofences.add(geofence);
// Build a request body.
GeofenceRequest.Builder geofenceRequestBuilder = new GeofenceRequest.Builder();
geofenceRequestBuilder.createGeofenceList(geofences);
geofenceRequestBuilder.setCoordinateType(Geofence.ENTER_GEOFENCE_CONVERSION | Geofence.EXIT_GEOFENCE_CONVERSION
| Geofence.DWELL_GEOFENCE_CONVERSION);
GeofenceRequest geofencingRequest = geofenceRequestBuilder.build();
// Create PendingIntent for the response.
Intent intent = new Intent();
int requestCode = 1;
Context context = this;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Initialize GeofenceService.
GeofenceService geofenceService = LocationServices.getGeofenceService(context); // Create a fence.
Task<Void> voidTask = geofenceService.createGeofenceList(geofencingRequest, pendingIntent);
voidTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); // Delete a fence.
List<String> requestIds = new ArrayList<>();
requestIds.add(requestId);
Task<Void> deleteTask = geofenceService.deleteGeofenceList(requestIds);
//Task<Void> deleteTask = geofenceService.deleteGeofenceList(pendingIntent); // A fence can also be removed using this method.
deleteTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); SettingsApi is built using GoogleApiClient. The latest Google APIs related to GoogleApiClient have been deprecated, and SettingsApi is no exception. You are advised to use the latest APIs of SettingsClient to build your project.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
SettingsApi.checkLocationSettings(GoogleApiClient googleApiClient,LocationSettingsRequest request) | org.xms.g.location.SettingsClient.checkLocationSettings(org.xms.g.location.LocationSettingsRequest request) | com.huawei.hms.location.SettingsClient.checkLocationSettings(com.huawei.hms.location.LocationSettingsRequest request) |
Context context = this;
SettingsApi settingsApi = LocationServices.SettingsApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
// ... Construct builder parameters.
LocationSettingsRequest locationSettingsRequest = builder.build();
settingsApi.checkLocationSettings(googleApiClient, locationSettingsRequest); Context context = this;
SettingsClient settingsClient = LocationServices.getSettingsClient(context);
Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(locationSettingsRequest);
task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
if (task.isSuccessful()) {
LocationSettingsResponse locationSettingsResponse = task.getResult();
// ...
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ...
}
}
}
}); FusedLocationProviderApi has been deprecated. The following describes the conversion between the deprecated functions and existing implementation functions. Expired attributes in LocationServices are not provided. You need to manually convert the attributes based on the used function. For example, if you use FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationCallback callback, Looper looper) to obtain location information, you need to use LocationServices.FusedLocationApi to build a request entity and use LocationServices.API to create the GoogleApiClient parameter object, after which the request can be initiated. However, in the Add HMS API and To HMS API scenarios, you need to use the latest request method FusedLocationProviderClient.requestLocationUpdates(LocationRequest locationRequest, LocationCallback callback, Looper looper) to obtain location information. For details about manual conversion, please refer to the corresponding function description.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
LocationServices.API | Not provided | Not provided |
LocationServices.FusedLocationApi | Not provided | Not provided |
LocationServices.GeofencingApi | Not provided | Not provided |
LocationServices.SettingsApi | Not provided | Not provided |
The Google API FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationCallback callback, Looper looper) has been deprecated. You are not advised to use this function to request location information. Instead, use the FusedLocationProviderClient.requestLocationUpdates(LocationRequest locationRequest, LocationCallback callback, Looper looper) function for replacement. The implementation is as follows.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationCallback callback, Looper looper) | org.xms.g.location.FusedLocationProviderClient.requestLocationUpdates(org.xms.g.location.LocationRequest locationRequest, org.xms.g.location.LocationCallback callback, Looper looper) | com.huawei.hms.location.FusedLocationProviderClient.requestLocationUpdates(com.huawei.hms.location.LocationRequest locationRequest, com.huawei.hms.location.LocationCallback callback, Looper looper) |
Context context = this;
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
LocationRequest locationRequest = new LocationRequest();
// ... Construct locationRequest parameters.
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
// ... Do your logic
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
// ... Do your logic
}
};
PendingResult<Status> statusPendingResult = fusedLocationProviderApi.requestLocationUpdates(googleApiClient,
locationRequest, locationCallback, Looper.getMainLooper()); Context context = this;
LocationRequest locationRequest = new LocationRequest();
// ... Construct locationRequest parameters.
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
// ... Do your logic
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
// ... Do your logic
}
};
Task<Void> voidTask = LocationServices.getFusedLocationProviderClient(context)
.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
voidTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); The following recording shows the process of using the latest API to implement FusedLocationProviderApi.requestLocationUpdates in the Add HMS API scenario.

The Google API FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent) has been deprecated. You are not advised to use this function to request location information. Instead, use the FusedLocationProviderClient.requestLocationUpdates(LocationRequest request, PendingIntent pendingIntent) function for replacement. The implementation is as follows.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent) | org.xms.g.location.FusedLocationProviderClient.requestLocationUpdates(org.xms.g.location.LocationRequest request, PendingIntent pendingIntent) | com.huawei.hms.location.FusedLocationProviderClient.requestLocationUpdates(com.huawei.hms.location.LocationRequest request, PendingIntent pendingIntent) |
Context context = this;
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
LocationRequest locationRequest = new LocationRequest();
// ... Construct locationRequest parameters.
Intent intent = new Intent();
int requestCode = 1;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingResult<Status> statusPendingResult6 =
fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, pendingIntent);
// ... Do your logic Context context = this;
LocationRequest locationRequest = new LocationRequest();
// ... Construct locationRequest parameters.
Intent intent = new Intent();
int requestCode = 1;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Task<Void> voidTask3 = LocationServices.getFusedLocationProviderClient(context)
.requestLocationUpdates(locationRequest, pendingIntent);
voidTask3.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); The Google API FusedLocationProviderApi.removeLocationUpdates(GoogleApiClient googleApiClient, PendingIntent pendingIntent) has been deprecated. You are not advised to use this function to send a request for removing location information. Instead, use the FusedLocationProviderClient.removeLocationUpdates(PendingIntent pendingIntent) function for replacement. The implementation is as follows.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.removeLocationUpdates(GoogleApiClient googleApiClient, PendingIntent pendingIntent) | org.xms.g.location.FusedLocationProviderClient.removeLocationUpdates(PendingIntent pendingIntent) | com.huawei.hms.location.FusedLocationProviderClient.removeLocationUpdates(PendingIntent pendingIntent) |
Deprecated implementation in FusedLocationProviderApi:
Context context = this;
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
Intent intent = new Intent();
int requestCode = 1;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingResult<Status> statusPendingResult =
fusedLocationProviderApi.removeLocationUpdates(googleApiClient, pendingIntent); Context context = this;
Intent intent = new Intent();
int requestCode = 1;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Task<Void> voidTask = LocationServices.getFusedLocationProviderClient(context).removeLocationUpdates(pendingIntent);
voidTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); The Google API FusedLocationProviderApi.removeLocationUpdates(GoogleApiClient client, LocationCallback callback) has been deprecated. You are not advised to use this function to send a request for removing location information. Instead, use the FusedLocationProviderClient.removeLocationUpdates(LocationCallback callback) function for replacement. The implementation is as follows.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.removeLocationUpdates(GoogleApiClient client, LocationCallback callback) | org.xms.g.location.FusedLocationProviderClient.removeLocationUpdates(org.xms.g.location.LocationCallback callback) | com.huawei.hms.location.FusedLocationProviderClient.removeLocationUpdates(com.huawei.hms.location.LocationCallback callback) |
Context context = this;
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
// ... Do your logic
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
// ... Do your logic
}
};
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
PendingResult<Status> statusPendingResult =
fusedLocationProviderApi.removeLocationUpdates(googleApiClient, locationCallback);
// ... Do your logic Context context = this;
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
// ... Do your logic
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
// ... Do your logic
}
};
Task<Void> voidTask =
LocationServices.getFusedLocationProviderClient(context).removeLocationUpdates(locationCallback);
voidTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); In the latest Google APIs, the LocationListener API has been removed and can no longer be used in any scenario. Use the LocationCallback method as a replacement of the API to send and remove location requests.
The following APIs have been deprecated.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.removeLocationUpdates(GoogleApiClient client, LocationListener listener) FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationListener istener) | org.xms.g.location.FusedLocationProviderClient.removeLocationUpdates(org.xms.g.location.LocationCallback callback) | com.huawei.hms.location.FusedLocationProviderClient.removeLocationUpdates(com.huawei.hms.location.LocationCallback callback) |
FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationListener listener,Looper looper) | org.xms.g.location.FusedLocationProviderClient.requestLocationUpdates(org.xms.g.location.LocationRequest locationRequest, org.xms.g.location.LocationCallback callback, Looper looper) | com.huawei.hms.location.FusedLocationProviderClient.requestLocationUpdates(com.huawei.hms.location.LocationRequest locationRequest, com.huawei.hms.location.LocationCallback callback, Looper looper) |
LocationCallback is used to send and remove requests as follows:
Context context = this;
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
LocationRequest locationRequest = new LocationRequest();
// ... Construct locationRequest parameters.
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
// ... Do your logic
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
// ... Do your logic
}
}; Task<Void> voidTask = fusedLocationProviderClient.requestLocationUpdates(locationRequest,
locationCallback, Looper.getMainLooper());
voidTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); Task<Void> voidTask = fusedLocationProviderClient.removeLocationUpdates(locationCallback);
voidTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); The Google API FusedLocationProviderApi.flushLocations(GoogleApiClient client) has been deprecated. HMS does not support this API.
The Google API FusedLocationProviderApi.getLastLocation(GoogleApiClient client) has been deprecated. You are not advised to use this function to obtain location information. Instead, use the FusedLocationProviderClient.getLastLocation() function for replacement. The implementation is as follows.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.getLastLocation(GoogleApiClient client) | org.xms.g.location.FusedLocationProviderClient.getLastLocation() | com.huawei.hms.location.FusedLocationProviderClient.getLastLocation() |
Context context = this;
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
Location location = fusedLocationProviderApi.getLastLocation(googleApiClient);
// ... Do your logic Context context = this;
Task<Location> locationTask = LocationServices.getFusedLocationProviderClient(context).getLastLocation();
locationTask.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
Location location = task.getResult();
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); The Google API FusedLocationProviderApi.getLocationAvailability(GoogleApiClient client) has been deprecated. You are not advised to use this function to obtain location availability information. Instead, use the FusedLocationProviderClient.getLocationAvailability() function for replacement. The implementation is as follows.
Google API (Deprecated) | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.getLocationAvailability(GoogleApiClient client) | org.xms.g.location.FusedLocationProviderClient.getLocationAvailability() | com.huawei.hms.location.FusedLocationProviderClient.getLocationAvailability() |
Context context = this;
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
LocationAvailability locationAvailability = fusedLocationProviderApi.getLocationAvailability(googleApiClient);
// ... Do your logic Context context = this;
Task<LocationAvailability> locationAvailabilityTask =
LocationServices.getFusedLocationProviderClient(context).getLocationAvailability();
locationAvailabilityTask.addOnCompleteListener(new OnCompleteListener<LocationAvailability>() {
@Override
public void onComplete(@NonNull Task<LocationAvailability> task) {
if (task.isSuccessful()) {
LocationAvailability locationAvailability = task.getResult();
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); The Google API FusedLocationProviderApi.setMockLocation(GoogleApiClient client, Location mockLocation) has been deprecated. You are not advised to use this function to set the mock location. Instead, use the FusedLocationProviderClient.setMockLocation(Location mockLocation) function for replacement. The implementation is as follows.
Google API | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.setMockLocation(GoogleApiClient client, Location mockLocation) | org.xms.g.location.FusedLocationProviderClient.setMockLocation(Location mockLocation) | com.huawei.hms.location.FusedLocationProviderClient.setMockLocation(Location mockLocation) |
Context context = this;
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
Location location = new Location("provider");
location.setLatitude(36.666666);
location.setLongitude(118.888888);
PendingResult<Status> statusPendingResult = fusedLocationProviderApi.setMockLocation(googleApiClient, location);
// ... Do your logic Context context = this;
Task<Void> voidTask = LocationServices.getFusedLocationProviderClient(context).setMockLocation(location);
voidTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); The Google API FusedLocationProviderApi.setMockMode(GoogleApiClient client, boolean isMockMode) has been deprecated. You are not advised to use this function to set the mock mode. Instead, use the FusedLocationProviderClient.setMockMode(boolean isMockMode) function for replacement. The implementation is as follows.
Google API | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.setMockMode(GoogleApiClient client, boolean isMockMode) | org.xms.g.location.FusedLocationProviderClient.setMockMode(boolean isMockMode) | com.huawei.hms.location.FusedLocationProviderClient.setMockMode(boolean isMockMode) |
Context context = this;
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
Api<Api.ApiOptions.NoOptions> api = LocationServices.API;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(api)
.addConnectionCallbacks(...)
.addOnConnectionFailedListener(...)
.build();
boolean shouldMock = false;
PendingResult<Status> statusPendingResult = fusedLocationProviderApi.setMockMode(googleApiClient, shouldMock); Context context = this;
boolean shouldMock = false;
Task<Void> voidTask5 = LocationServices.getFusedLocationProviderClient(context).setMockMode(shouldMock);
voidTask5.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// ... Do your logic
} else {
Exception exception = task.getException();
if (exception != null) {
String message = exception.getMessage();
// ... Do your logic
}
}
}
}); Google API | Add HMS API | To HMS API |
|---|---|---|
FusedLocationProviderApi.KEY_LOCATION_CHANGED | Not provided | Not provided |
FusedLocationProviderApi.KEY_MOCK_LOCATION | Not provided | Not provided |
These constants have been deprecated in the latest APIs and are not provided in the latest version.
The LocationStatusCodes class has been deprecated. The following table lists the mapping of deprecated status codes and their replacements.
Type | LocationStatusCodes | GeofenceErrorCodes |
|---|---|---|
Constant | int ERROR = 1 | int ERROR = 13 |
Constant | int GEOFENCE_NOT_AVAILABLE = 1000 | int GEOFENCE_UNAVAILABLE = 10200 |
Constant | int GEOFENCE_TOO_MANY_GEOFENCES = 1001 | int GEOFENCE_NUMBER_OVER_LIMIT = 10201 |
Constant | int GEOFENCE_TOO_MANY_PENDING_INTENTS = 1002 | int GEOFENCE_PENDINGINTENT_OVER_LIMIT = 10202 |
Constant | int SUCCESS = 0 | int SUCCESS = 0 |