我在我的应用程序中使用LocationServices
。在使用位置服务之前,我试图验证具有必需设置的“位置”是否为ON,但是我面临的问题是SettingsClient.checkSettings
始终为failing
。请查看我的LocationRequest
和LocationSettingRequest
构建器:
位置请求
mLocationRequest = LocationRequest()
mLocationRequest.interval = interval
mLocationRequest.fastestInterval = interval
Log.v(TAG, "distance => $distance")
if(distance > 0) {
mLocationRequest.smallestDisplacement = distance
}
mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
buildLocationSettingsRequest()
LocationSettingRequestBuilder
private fun buildLocationSettingsRequest() {
val builder = LocationSettingsRequest.Builder()
builder.addLocationRequest(mLocationRequest)
mLocationSettingsRequest = builder.build()
mSettingsClientInit = true
}
我要求
checkSettings
为mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(this, object : OnSuccessListener<LocationSettingsResponse> {
override fun onSuccess(locationSettingsResponse: LocationSettingsResponse) {
Log.i(TAG, "LocationManager: All location settings are satisfied.");
mLocationCallback?.let {
fusedLocationClient?.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
// updateUI();
}
})
.addOnFailureListener(this, object : OnFailureListener {
override fun onFailure(e: Exception) {
Log.v(TAG, "Request PErmission failure");
var statusCode = (e as ApiException).getStatusCode()
when (statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
var rae = e as ResolvableApiException;
rae.startResolutionForResult(this@BaseLocationActivity, REQUEST_CHECK_SETTINGS);
} catch (sie: IntentSender.SendIntentException) {
Log.v(TAG, "PendingIntent unable to execute request.");
}
}
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
mRequestingLocationUpdates = false;
Log.v(TAG, "Settings change unavailable.");
}
}
}
}
)
它始终是
onFailure
的addOnFailureListener
结果。谁能帮助我,这可能是什么问题?此设置对话框显示“确定”以启用设置,我按“确定”,再次出现与onFailure回调相同的对话框。最佳答案
您好,请按照以下代码解决您的问题,我在此类的帮助下添加了GPSUtils类,您可以轻松管理GPS。
GpsUtils.Class
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.IntentSender;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import static android.content.ContentValues.TAG;
public class GpsUtils {
private Context context;
private SettingsClient mSettingsClient;
private LocationSettingsRequest mLocationSettingsRequest;
private LocationManager locationManager;
private LocationRequest locationRequest;
public GpsUtils(Context context) {
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
mSettingsClient = LocationServices.getSettingsClient(context);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10 * 1000);
locationRequest.setFastestInterval(2 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
mLocationSettingsRequest = builder.build();
//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************
}
// method for turn on GPS
public void turnGPSOn(onGpsListener onGpsListener) {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (onGpsListener != null) {
onGpsListener.gpsStatus(true);
}
} else {
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener((Activity) context, new OnSuccessListener<LocationSettingsResponse>() {
@SuppressLint("MissingPermission")
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// GPS is already enable, callback GPS status through listener
if (onGpsListener != null) {
onGpsListener.gpsStatus(true);
}
}
})
.addOnFailureListener((Activity) context, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult((Activity) context, AppConstants.GPS_REQUEST);
} catch (IntentSender.SendIntentException sie) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
String errorMessage = "Location settings are inadequate, and cannot be " +
"fixed here. Fix in Settings.";
Log.e(TAG, errorMessage);
Toast.makeText((Activity) context, errorMessage, Toast.LENGTH_LONG).show();
}
}
});
}
}
public interface onGpsListener {
void gpsStatus(boolean isGPSEnable);
}
}
GPS对话框将显示SettingsClient的失败回调,并会导致 Activity 的onActivityResult()。因此,基本上,如果用户同意打开GPS,我们可以在 Activity 中启用GPS标志。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == AppConstants.GPS_REQUEST) {
isGPS = true; // flag maintain before get location
}
}
}
现在在获取位置之前调用GpsUtils类。
new GpsUtils(this).turnGPSOn(new GpsUtils.onGpsListener() {
@Override
public void gpsStatus(boolean isGPSEnable) {
// turn on GPS
isGPS = isGPSEnable;
}
});