gpt4 book ai didi

android - 当 gps 关闭时,lastKnownLocation 总是返回 null

转载 作者:行者123 更新时间:2023-11-29 00:07:01 26 4
gpt4 key购买 nike

我正在使用 Google 服务 API LocationServices.FusedLocationApi 来查找用户的当前位置,然后更新位置。我已经在模拟器和实际设备上进行了测试,我发现如果我关闭 GPS LocationServices.FusedLocationApi.getLastLocation() 总是返回 null,但是如果我打开 GPS,我会得到一个有效值。这是我正在使用的代码:

private GoogleApiClient mGoogleApiClient;
private Location mLastKnownLocation;
mLastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (mLastKnownLocation != null) {
Log.i(TAG, String.valueOf(mLastKnownLocation.getLatitude()));
Log.i(TAG, String.valueOf(mLastKnownLocation.getLongitude()));
}

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, createLocationRequest(), this);

我是不是漏掉了什么?提前致谢。

最佳答案

我认为你应该首先使用这个检查你的设备是否有 GPS:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}

private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}

然后使用 LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)

更多信息和代码请引用here .

关于android - 当 gps 关闭时,lastKnownLocation 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33121177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com