简体   繁体   English

当 GPS 关闭时,.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 总是返回 false?

[英].isProviderEnabled(LocationManager.NETWORK_PROVIDER) returns always false when gps is off?

Is it possible to get the location from NETWORK if GPS is OFF ?如果 GPS 关闭,是否可以从 NETWORK 获取位置?

My Demo can get the current user-location from NETWORK and from GPS but only if GPS is ON.我的演示可以从网络和 GPS 获取当前用户位置,但前提是 GPS 开启。

If I try to get the location from LastKnownLocation, I get null too.如果我尝试从 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");
}
}

I have tested this code below.我在下面测试了这段代码。 it returns the location null too, if I make the device-gps off : GPSTracker.java如果我关闭设备gps,它也会返回位置null: GPSTracker.java

If you are trying to request the location from the network as a fallback to GPS, you will need to have ACCESS_COARSE_LOCATION enabled.如果您尝试从网络请求位置作为 GPS 的后备,则需要启用ACCESS_COARSE_LOCATION Also, even with the permission enabled, you must request a location update, to refresh and update the last network position.此外,即使启用了权限,您也必须请求位置更新,以刷新和更新最后的网络位置。 Only after that, you'll have the last known location available , otherwise, you'll get null indeed.只有在那之后,您才会拥有最后一个已知位置 available ,否则,您确实会得到 null 。

To make your code work, you probably just need to call your GPSTracker.getLocation() before your line with getSystemService(Context.LOCATION_SERVICE);为了使您的代码工作,您可能只需要在使用getSystemService(Context.LOCATION_SERVICE);之前调用GPSTracker.getLocation() getSystemService(Context.LOCATION_SERVICE);

You can check the documentation for getLastKnownLocation here: https://developer.android.com/reference/android/location/LocationManager#getLastKnownLocation(java.lang.String)您可以在此处查看getLastKnownLocation的文档: https : //developer.android.com/reference/android/location/LocationManager#getLastKnownLocation ( getLastKnownLocation )

Gets the last known location from the given provider, or null if there is no last known location.从给定的提供程序获取最后一个已知位置,如果没有最后一个已知位置,则为 null。 The returned location may be quite old in some circumstances, so the age of the location should always be checked.在某些情况下,返回的位置可能很旧,因此应始终检查位置的年龄。

👉 This will never activate sensors to compute a new location, and will only ever return a cached location. 👉这永远不会激活传感器来计算新位置,并且只会返回缓存的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM