目前Google API中使用GoogleApiClient类的API均已被废弃,在Add HMS API和To HMS API的场景下,都需要您进行手工转换。以下内容可以帮助您将这些已废弃的API手工转换成最新的实现。
Google API | Add HMS API | To HMS API |
|---|---|---|
new DetectedActivity(activityType, confidence) | ActivityIdentificationData(identificationActivity, possibility) 说明:构造方法抛出新异常ApiException,表示ActivityIdentificationData构造对象失败,您自行处理。 | new ActivityIdentificationData(identificationActivity, possibility) 说明:方法抛出新异常ApiException,表示ActivityIdentificationData构造对象失败,您自行处理。 |
DetectedActivity()的实现如下:
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); }
在Add HMS API场景下,推荐使用以下实现方式:
//请注意导入的包名 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); }
ActivityRecognition中有关Google API的接口和常量都已经被移除,推荐使用最新的实现函数来构建您的项目。
Google API(Deprecated) | Add HMS API | To HMS API |
|---|---|---|
ActivityRecognition.API | 不提供 | 不提供 |
ActivityRecognition.ActivityRecognitionApi | 不提供 | 不提供 |
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); //请注意导入的包名
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);
// 发起请求
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
}
}
}
});
// 移除请求
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() + "%");
}
}
}
} 在To HMS API场景下,推荐使用以下实现方式:
//请注意导入的包名
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);
// 发起请求
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
}
}
}
});
// 移除请求
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() + "%");
}
}
}
} 在最新的Google API中GeofencingApi已完全被废弃,不建议使用已废弃接口。在Add HMS API和To HMS API的场景下,推荐使用最新的函数来处理对应的实现。
GeofencingApi中已废弃的API:
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) |
请使用最新的GeofencingClient类对地理围栏进行添加和移除,实现地理围栏功能。以下具体描述在Add HMS API和To HMS API场景下的实现。
Add HMS API 场景下的实现:
//请注意导入的包名
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;
//初始化请求参数
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;
//添加围栏到围栏列表
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);
//构建请求体
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();
//创建响应的PendingIntent
Intent intent = new Intent();
int requestCode = 1;
Context context = this;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//初始化GeofencingClient
GeofencingClient geofencingClient = LocationServices.getGeofencingClient(context); //创建围栏
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
}
}
}
}); //删除围栏
List<String> requestIds = new ArrayList<>();
requestIds.add(requestId);
Task<Void> removeTask = geofencingClient.removeGeofences(requestIds);
//Task<Void> removeTask = geofencingClient.removeGeofences(pendingIntent); //也可以使用此方式来移除围栏。
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
}
}
}
}); To HMS API场景下的实现:
//请注意导入的包名
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;
//初始化请求参数
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;
//添加围栏到围栏列表
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);
//构建请求体
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();
//创建响应的PendingIntent
Intent intent = new Intent();
int requestCode = 1;
Context context = this;
PendingIntent pendingIntent =
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//初始化GeofenceService
GeofenceService geofenceService = LocationServices.getGeofenceService(context); //创建围栏
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
}
}
}
}); //删除围栏
List<String> requestIds = new ArrayList<>();
requestIds.add(requestId);
Task<Void> deleteTask = geofenceService.deleteGeofenceList(requestIds);
//Task<Void> deleteTask = geofenceService.deleteGeofenceList(pendingIntent); //也可以使用此方式来移除围栏。
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是通过GoogleApiClient构建,最新的Google API中有关GoogleApiClient的API均已废弃,SettingsApi亦不例外,推荐使用SettingsClient最新接口来构建您的工程。
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();
// … builder的参数构造
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已废弃,以下说明其废弃函数和现有实现函数的转换。LocationServices中关于过期的属性将不会提供,请根据使用的函数进行手动切换,例如:使用FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationCallback callback, Looper looper)来获取位置信息,在使用该函数时,需使用LocationServices.FusedLocationApi构建请求实体,并通过LocationServices.API创建GoogleApiClient参数对象,才能正常发起请求。但是在Add HMS API和To HMS API的场景下,您需要使用最新的请求方式FusedLocationProviderClient.requestLocationUpdates(LocationRequest locationRequest, LocationCallback callback, Looper looper)来获取位置信息。详细的手工切换说明还请参见对应的函数说明。
Google API(Deprecated) | Add HMS API | To HMS API |
|---|---|---|
LocationServices.API | 不提供 | 不提供 |
LocationServices.FusedLocationApi | 不提供 | 不提供 |
LocationServices.GeofencingApi | 不提供 | 不提供 |
LocationServices.SettingsApi | 不提供 | 不提供 |
Google API已经废弃FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationCallback callback, Looper looper) 的使用,不建议使用该函数进行位置信息的请求,建议使用FusedLocationProviderClient.requestLocationUpdates(LocationRequest locationRequest, LocationCallback callback, Looper looper)函数进行替换处理,具体实现如下:
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();
// … locationRequest的参数构建
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();
// … locationRequest的参数构建
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
}
}
}
}); 以下动图展示了Add HMS API场景下使用最新的接口来实现FusedLocationProviderApi.requestLocationUpdates的过程:

Google API已经废弃FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent) 的使用,不建议使用该函数进行位置信息的请求,建议使用FusedLocationProviderClient.requestLocationUpdates(LocationRequest request, PendingIntent pendingIntent)函数进行替换处理,具体实现如下:
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();
// … locationRequest的参数构建
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();
// … locationRequest的参数构建
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
}
}
}
}); Google API已经废弃FusedLocationProviderApi.removeLocationUpdates(GoogleApiClient googleApiClient, PendingIntent pendingIntent) 的使用,不建议使用该函数进行移除位置信息的请求,建议使用FusedLocationProviderClient.removeLocationUpdates(PendingIntent pendingIntent)函数进行替换处理,具体实现如下:
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) |
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
}
}
}
}); Google API已经废弃FusedLocationProviderApi.removeLocationUpdates(GoogleApiClient client, LocationCallback callback) 的使用,不建议使用该函数进行移除位置信息的请求,建议使用FusedLocationProviderClient.removeLocationUpdates(LocationCallback callback)函数进行替换处理,具体实现如下:
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
}
}
}
}); 在最新的Google API中,LocationListener该接口已经被完全移除,不再支持有关该接口使用的场景。请使用LocationCallback回调的方式进行发送请求和移除对应的定位请求来替换这类API使用的场景。
废弃的API如下:
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回调的请求方式如下:
Context context = this;
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
LocationRequest locationRequest = new LocationRequest();
// … locationRequest的参数构建
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
}
}
}
}); Google API已经废弃FusedLocationProviderApi.flushLocations(GoogleApiClient client) 的使用,HMS不支持该接口。
Google API已经废弃FusedLocationProviderApi.getLastLocation(GoogleApiClient client)的使用,不建议使用该函数进行获取位置信息的请求,建议使用FusedLocationProviderClient.getLastLocation()函数进行替换处理,具体实现如下:
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
}
}
}
}); Google API已经废弃FusedLocationProviderApi.getLocationAvailability(GoogleApiClient client)的使用,不建议使用该函数进行位置信息是否可用的请求,建议使用FusedLocationProviderClient.getLocationAvailability()函数进行替换处理,具体实现如下:
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
}
}
}
}); Google API已经废弃FusedLocationProviderApi.setMockLocation(GoogleApiClient client, Location mockLocation) 的使用,不建议使用该函数进行模拟位置的设置,建议使用FusedLocationProviderClient.setMockLocation(Location mockLocation)函数进行替换处理,具体实现如下:
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
}
}
}
}); Google API已经废弃FusedLocationProviderApi.setMockMode(GoogleApiClient client, boolean isMockMode) 的使用,不建议使用该函数进行模拟位置模式的设置,建议使用FusedLocationProviderClient.setMockMode(boolean isMockMode)函数进行替换处理,具体实现如下:
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 | 不提供 | 不提供 |
FusedLocationProviderApi.KEY_MOCK_LOCATION | 不提供 | 不提供 |
在最新的API中该常量已经被废弃,最新版本不再提供。
LocationStatusCodes类已被完全废弃,状态码对应如下表:
类型 | LocationStatusCodes | GeofenceErrorCodes |
|---|---|---|
常量 | int ERROR = 1 | int ERROR = 13 |
常量 | int GEOFENCE_NOT_AVAILABLE = 1000 | int GEOFENCE_UNAVAILABLE = 10200 |
常量 | int GEOFENCE_TOO_MANY_GEOFENCES = 1001 | int GEOFENCE_NUMBER_OVER_LIMIT = 10201 |
常量 | int GEOFENCE_TOO_MANY_PENDING_INTENTS = 1002 | int GEOFENCE_PENDINGINTENT_OVER_LIMIT = 10202 |
常量 | int SUCCESS = 0 | int SUCCESS = 0 |