/*
* Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.huawei.locationsample6.location.fusedlocation;
import java.util.List;
import android.location.Location;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.location.FusedLocationProviderClient;
import com.huawei.hms.location.HWLocation;
import com.huawei.hms.location.LocationAvailability;
import com.huawei.hms.location.LocationCallback;
import com.huawei.hms.location.LocationRequest;
import com.huawei.hms.location.LocationResult;
import com.huawei.hms.location.LocationServices;
import com.huawei.hms.location.LocationSettingsRequest;
import com.huawei.hms.location.LocationSettingsResponse;
import com.huawei.hms.location.SettingsClient;
import com.huawei.locationsample6.LogInfoUtil;
import com.huawei.locationsample6.R;
import com.huawei.locationsample6.util.NotificationUtil;
import com.huawei.logger.LocationLog;
/**
* 华为融合定位
*
*/
public class RequestLocationUpdatesWithCallbackActivity extends LocationBaseActivity implements OnClickListener {
public static final String TAG = "LocationUpdatesCallback";
LocationCallback mLocationCallback;
LocationRequest mLocationRequest;
private FusedLocationProviderClient mFusedLocationProviderClient;
private SettingsClient mSettingsClient;
private FusedLocationProviderClient fusedLocationProviderClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_request_location_updates_callback);
// Button click listeners
findViewById(R.id.location_requestLocationUpdatesWithCallback).setOnClickListener(this);
findViewById(R.id.location_removeLocationUpdatesWithCallback).setOnClickListener(this);
findViewById(R.id.location_enableBackgroundLocation).setOnClickListener(this);
findViewById(R.id.location_disableBackgroundLocation).setOnClickListener(this);
addLogFragment();
//TODO 创建网络定位对象FusedLocationProviderClient
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// 创建SettingsClient对象
mSettingsClient = LocationServices.getSettingsClient(this);
//TODO 创建一个LocationRequest 对象,并将其定位模式设置位持续定位,并且设置其返回的坐标类型为:LocationRequest.COORDINATE_TYPE_WGS84。
LocationRequest mLocationRequest = new LocationRequest();
// 设置位置更新的间隔(单位:毫秒)
mLocationRequest.setInterval(10000);
// 设置定位类型
mLocationRequest.setPriority(LocationRequest.COORDINATE_TYPE_WGS84);
//TODO 添加定位更新回调,并在回调结果中打印出所有回调的位置的Latitude和Longitude
// LocationCallback mLocationCallback;
// mLocationCallback = new LocationCallback() {
// @Override
// public void onLocationResult(LocationResult locationResult) {
// if (locationResult != null) {
// // TODO: 处理位置回调结果
// locationResult.getLocations();
// }
// }
// };
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
// TODO: 处理位置回调结果
HWLocation hwLocation = locationResult.getLastHWLocation();
hwLocation.getLatitude();
hwLocation.getLongitude();
}
}
};
}
/**
* 请求定位
*/
private void requestLocationUpdatesWithCallback() {
Log.i(TAG, "requestLocationUpdatesWithCallback");
LogInfoUtil.getLogInfo(this);
//TODO 调用FusedLocationProviderClient对象的requestLocationUpdates()进行定位。
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper());
}
@Override
protected void onDestroy() {
// Removed when the location update is no longer required.
removeLocationUpdatesWithCallback();
disableBackgroundLocation();
super.onDestroy();
}
/**
* 移除请求定位
*/
private void removeLocationUpdatesWithCallback() {
//TODO 使用mFusedLocationProviderClient调用removeLocationUpdates()停止位置更新。
mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback)
// 停止位置更新成功监听回调
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// TODO: 停止位置更新成功的处理
Toast.makeText(RequestLocationUpdatesWithCallbackActivity.this, "", Toast.LENGTH_SHORT).show();
}
})
// 停止位置更新失败监听回调
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// TODO:停止位置更新失败的处理
Log.e(TAG,"失败" + e.getMessage());
}
});
}
/**
* 启动后台定位模式
*/
private void enableBackgroundLocation() {
mFusedLocationProviderClient
.enableBackgroundLocation(NotificationUtil.NOTIFICATION_ID, NotificationUtil.getNotification(this))
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
LocationLog.i(TAG, "enableBackgroundLocation onSuccess");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
LocationLog.e(TAG, "enableBackgroundLocation onFailure:" + e.getMessage());
}
});
}
/**
* 解除后台定位模式
*/
private void disableBackgroundLocation() {
mFusedLocationProviderClient.disableBackgroundLocation()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
LocationLog.i(TAG, "disableBackgroundLocation onSuccess");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
LocationLog.e(TAG, "disableBackgroundLocation onFailure:" + e.getMessage());
}
});
}
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.location_requestLocationUpdatesWithCallback:
requestLocationUpdatesWithCallback();
break;
case R.id.location_removeLocationUpdatesWithCallback:
removeLocationUpdatesWithCallback();
break;
case R.id.location_enableBackgroundLocation:
enableBackgroundLocation();
break;
case R.id.location_disableBackgroundLocation:
disableBackgroundLocation();
break;
default:
break;
}
} catch (Exception e) {
Log.e(TAG, "RequestLocationUpdatesWithCallbackActivity Exception:" + e);
}
}
}
融合定位服务
最新推荐文章于 2024-08-21 22:27:57 发布