Java Android requestLocationUpdates带有GPS null

Java Android requestLocationUpdates带有GPS null,java,android,android-asynctask,android-location,android-gps,Java,Android,Android Asynctask,Android Location,Android Gps,} 即使我检查GPS是否打开,当我尝试使用GPS位置时,我总是得到空值,然后它从网络获取位置。 出于这个原因,我试图将GPS设置为“仅GPS”,但我得到了空值,因此没有位置。 我读了所有其他帖子,但我一直在GPS上取空。我正在使用USB调试在真实设备上进行模拟 有什么想法吗?getLastKnownLocation()经常返回null。这是完全正常的。您只能使用getLastKnownLocation(): 作为优化,避免必须等待位置修复,或 如果你没有位置数据也能生活 否则,您需要推迟HT

}

即使我检查GPS是否打开,当我尝试使用GPS位置时,我总是得到空值,然后它从网络获取位置。 出于这个原因,我试图将GPS设置为“仅GPS”,但我得到了空值,因此没有位置。 我读了所有其他帖子,但我一直在GPS上取空。我正在使用USB调试在真实设备上进行模拟

有什么想法吗?

getLastKnownLocation()
经常返回
null
。这是完全正常的。您只能使用
getLastKnownLocation()

  • 作为优化,避免必须等待位置修复,或
  • 如果你没有位置数据也能生活
否则,您需要推迟HTTP GET请求,直到在
onLocationChanged()
中获得给定的位置。请记住,您可能永远不会得到位置定位


LocationManager的正确使用参见。

我相信您对LocationManager的工作原理的解释是错误的

当您调用
locationManager.requestLocationUpdates(bestProvider,0,0,locationListener)
时,您只是注册以接收位置更新,这通常需要一些时间才能发生(并且可能永远不会发生,正如Commonware指出的那样)。此调用不会立即为您提供更新的位置,因此,在下一行代码中,当您调用
getLastKnownLocation()
时,接收
null
是正常行为

也就是说,我认为处理你的情况的最好办法是:

1-首先:检查
getLastKnownLocation()
是否返回null(如果所选LocationProvider已经从您的应用程序或其他应用程序中找到了最近的位置,它将在此处为您返回该位置。但请注意:此位置可能非常过时!)

2-如果
getLastKnownLocation()
返回null,那么您将没有其他选择,只能请求一个全新的位置并等待它到达,这将发生在LocationListener中的方法
onLocationChanged
上。因此,要做到这一点,您有两个选项:(a)调用
requestSingleUpdate()
,它将为您返回一次更新的位置,或者(b)调用
requestLocationUpdates()
,它将注册locationManager以接收定期的位置更新(并将消耗更多电池)

3-在这两个选项中,当您收到位置更新时,将调用LocationListener中的方法
onLocationChanged
,然后您可以通过此方法调用您的
LoadBusinesss
,并收到全新的位置

希望这是清楚的;)

-----编辑-----

在调用
requestSingleUpdate
requestLocationUpdates
之前,您还应该在运行时请求位置权限。为此,请使用以下代码段:

private void loadBusinesses(){
    gpsMsg.setVisibility(View.INVISIBLE);
    getLocation();
    currentView = GEOVIEW;
    businessesList.nextUrl = "null";
    if (!businessesList.isEmpty()){
        Log.e("businessList ","not empty");
        businessesList.clear();
        notifyAdapter();
    }

    Double latitude;
    Double longitude;
    try{
        latitude = mLocation.getLatitude();
        longitude = mLocation.getLongitude();
    } catch (NullPointerException e){
        Log.e("GPS", "Location unavailable");
        gpsMsg.setVisibility(View.VISIBLE);
        swipeContainer.setRefreshing(false);
        return;
    }
如果以前未授予该权限,系统将提示用户授予(或不授予)。因此,您还应该在活动中@Override方法
onRequestPermissionResult
,检查用户是否授予了权限,然后正确响应。这是您应该覆盖的方法:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // If permission is not granted, request it from user. When user responds, your activity will call method "onRequestPermissionResult", which you should override
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
} else {
    locationManager.requestSingleUpdate(bestProvider, locationListener, null)
}

关于
getLastKnownLocation()
的一点是,如果您在设置中更改位置提供程序,重新启动设备,或在设备上打开或关闭位置服务,最后一个已知位置将被清除,并将返回
null
。最重要的是,如果返回一个位置点,它可能太旧,可能与此无关。应使用
getLastKnownLocation()
函数作为提示

关于你的评论说,当你在室内使用GPS时,你什么也得不到,但谷歌地图仍然可以使用。。。谷歌地图使用,它同时使用GPS和网络为您提供最佳位置。如果你只在你的设备上使用GPS,打开谷歌地图,你会看到它给你一个坏点(灰点),或者根本没有点


我建议您根据您的位置需要使用提供商,因为它将为您节省电池,在您丢失GPS时提供可靠的备用方案,并且它具有自己最后已知的位置功能,似乎与
LocationManager

不相关。我这样使用您的建议:
LocationManager.requestSingleUpdate(bestProvider,locListen,null);
其中我的locListen有:
@覆盖公共void onLocationChanged(Location-Location){Log.e(“Single_-loc”,Location.getLongitude()+“”+Location.getLatitude());}
问题是,如果我使用emulator,我必须从菜单中发送位置,然后它读取位置,但如果我使用真正的设备,它什么也不做,它不会强制转换onLocationChanged、onStatusChanged、onProviderEnabled或onProviderDisabled。我还尝试使用谷歌地图,即使我在室内,它也能正常工作。你要求过吗d访问设备位置的权限?您应该在AndroidManifest中添加权限
access\u rough\u location
access\u FINE\u location
。此外,从Android 6.0开始,您必须在运行时请求此权限,然后再调用requestSingleUpdate。请在此处阅读:我将我的答案编辑为include如果是这样的话,请在运行时删除请求位置权限的代码段。
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // If permission is not granted, request it from user. When user responds, your activity will call method "onRequestPermissionResult", which you should override
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
} else {
    locationManager.requestSingleUpdate(bestProvider, locationListener, null)
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // PERMISSION WAS GRANTED
            } else {
                // USER DID NOT GRANT PERMISSION
            }
            break;
    }
}