【发布时间】:2021-03-04 07:21:41
【问题描述】:
如果 GPS 关闭,是否可以从 NETWORK 获取位置?
我的演示可以从 NETWORK 和 GPS 获取当前用户位置,但前提是 GPS 已打开。
如果我尝试从 LastKnownLocation 获取位置,我也会得到 null。
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
startActivity(new Intent(MainActivity.this, Permissions.class));
return;
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
isGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPS && !isNetwork) {
Log.d("TAG", "gps and network false");
}else {
if (isGPS) {
loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
Log.i("TAG", "gps loc found");}
}
if (isNetwork) {
loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc != null) {
Log.i("TAG", "network loc found");
}
}
我在下面测试了这段代码。如果我关闭 device-gps,它也会返回位置 null : GPSTracker.java
【问题讨论】:
-
您可以将代码发布到您检索位置的位置吗?另外,这里可能有类似的问题:stackoverflow.com/questions/20210565/…
-
我已经更新了代码,向您展示了我如何检索 loc。
-
我已经测试过这个 GPSTracker。如果 gps 关闭,它也会返回 null:GPSTracker.java
标签: java android android-gps