getLastKnownLocation()返回null

Xen*_*Sis 4 gps android google-maps android-location

我在这里已经阅读了很多有关此主题的问答,但我不得不说它们都不起作用.

我的问题是,即使我启用了GPS,我也无法获得位置,除非我打开谷歌地图并获取我的位置然后返回应用程序,这绝对不是用户的选择.

我有以下功能来获取位置.

public Location getCurrentLocation() {
    LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location myLocation = locationManager.getLastKnownLocation(provider);

    return myLocation;
}
Run Code Online (Sandbox Code Playgroud)

有什么我不知道如何解决这个问题?我也试过这个 http://developer.android.com/training/location/retrieve-current.html#last-known 但仍然返回null.

提前致谢

Com*_*are 8

有什么我不知道如何解决这个问题?

GPS无线电正常断电,因为它们是主要的电池消耗.因此,getLastKnownLocation()可以经常返回null或陈旧的位置,因为没有检查位置修复.getLastKnownLocation()因此,只有在您对该地点有不经意的兴趣时才有用,如果没有地点则感到高兴.

如果您需要知道位置,您将需要使用requestLocationUpdates()或类似的东西,以使Android启动GPS无线电并积极尝试找到位置.