Android Kotlin-从网络提供商处获取位置时出现问题
我必须在Android应用程序中从用户设备获取位置 我做了如下工作: 首先,我已检查运行时位置权限。拍摄成功 秒,检查GPS是否启用,如果GPS提供商工作正常,我可以从GPS提供商获取纬度和经度 第三个,如果GPS关闭,我将尝试从网络提供商处获取位置。 但是,在这种情况下,有时我无法成功获取位置 所以,我必须让用户打开GPS,并在打开GPS后获得位置 我很困惑,因为许多演示提供了打开GPS的意图。 如何动态打开GPS? 如何通知用户已打开GPSAndroid Kotlin-从网络提供商处获取位置时出现问题,android,kotlin,gps,location,android-gps,Android,Kotlin,Gps,Location,Android Gps,我必须在Android应用程序中从用户设备获取位置 我做了如下工作: 首先,我已检查运行时位置权限。拍摄成功 秒,检查GPS是否启用,如果GPS提供商工作正常,我可以从GPS提供商获取纬度和经度 第三个,如果GPS关闭,我将尝试从网络提供商处获取位置。 但是,在这种情况下,有时我无法成功获取位置 所以,我必须让用户打开GPS,并在打开GPS后获得位置 我很困惑,因为许多演示提供了打开GPS的意图。 如何动态打开GPS? 如何通知用户已打开GPS 谢谢。这是Gradle插件,就我而言,它的版本是:
谢谢。这是Gradle插件,就我而言,它的版本是:“17.0.0”
implementation "com.google.android.gms:play-services-location:$parent.ext.playServicesVersion"
这是GpsUtils
类
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.IntentSender;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
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;
public class GpsUtils {
private Context context;
private SettingsClient mSettingsClient;
private LocationSettingsRequest mLocationSettingsRequest;
private LocationManager locationManager;
private static final String TAG = GpsUtils.class.getSimpleName();
public GpsUtils(Context context) {
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
mSettingsClient = LocationServices.getSettingsClient(context);
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(2 * 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 {
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.";
Toast.makeText((Activity) context, errorMessage, Toast.LENGTH_LONG).show();
}
}
});
}
}
public interface onGpsListener {
void gpsStatus(boolean isGPSEnable);
}
}
您可以从中阅读有关
设置客户端的更多信息。您好,先生,我找到了显示GPS对话框的方法。现在,问题是如何通知用户现在是否按下了OK键?如果他们按下OK
,最终您将启用GPS,反之亦然,对吗?点击“是”或“否”Btn,您希望实现什么?总之,addOnSuccessListener
和addOnFailureListener
可以帮助您吗?
GpsUtils(this).turnGPSOn { isGPSEnable ->
isGPS = isGPSEnable // You can get the callback here..
}