android getLaskKnownLocation null

android getLaskKnownLocation null,android,Android,我尝试了GPS和网络提供商,但他们为getLastKnownLocation提供了空位置。我也没有改变任何位置。我正在我的公寓里尝试,那里的手机信号很强。我甚至可以很好地浏览互联网,从市场上下载和安装应用程序 我读过几篇关于同一主题的文章,但他们的建议似乎我已经尝试过了 这是我的代码片段 public void onCreate() { // TODO Auto-generated method stub super.onCreate(); locationManage

我尝试了GPS和网络提供商,但他们为getLastKnownLocation提供了空位置。我也没有改变任何位置。我正在我的公寓里尝试,那里的手机信号很强。我甚至可以很好地浏览互联网,从市场上下载和安装应用程序

我读过几篇关于同一主题的文章,但他们的建议似乎我已经尝试过了

这是我的代码片段

 public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if(null == locationManager) {
        Log.d(TAG, "location manager NULL");
    }
    geocoder = new Geocoder(this); 

    // Initialize with the last known location
    Location lastLocation = locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    if (lastLocation != null)
        onLocationChanged(lastLocation);
    else
        Log.i(TAG, "null LastKnownLocation");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    super.onStartCommand(intent, flags, startId);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
            0,0, this);
    Log.d(TAG, "requested location updates");

    return START_STICKY;
}
根据日志:

mitenm@pinkydebian:/media/sf_E_DRIVE/tmp$ adb logcat LocationNotifierService:* *:S
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
I/LocationNotifierService(30866): null LastKnownLocation
D/LocationNotifierService(30866): gps provider enabled:true
D/LocationNotifierService(30866): network provider enabled:true
D/LocationNotifierService(30866): onCreate
D/LocationNotifierService(30866): requested location updates
D/LocationNotifierService(30866): requested location updates
D/LocationNotifierService(30866): requested location updates
我有解锁接收器,所以当我解锁我的屏幕时,我请求位置更新,这是从日志中预期的工作。但是不会调用onLocationChanged方法

我已经被添加了许可,以显示精细和粗糙

问候,

米滕

android getLaskKnownLocation null

根据谷歌文档

getLaskKnownLocation返回一个位置,该位置指示从给定提供程序获得的上一个已知位置修复中的数据,否则返回null

这意味着它显示缓存的数据。如果您在以前从未查询过位置数据的设备上运行应用程序,则缓存将为空,这就是为什么您会得到null

也不要更改任何联机位置

确保已为位置启用网络提供程序

使用下面的代码片段

if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected() || connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).isConnected()) {

    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        Constants.CONNECTED = true;

        locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 5000, 1, this);
    } else {
        Constants.CONNECTED = false;

        Toast.makeText(
        context, "Please enable Wireless networks in Location Setting!!", 10000).show();
    }

}

是否在设备上启用了位置服务,是否在清单中添加了权限,如果是,是否发布日志跟踪,是否会出现任何错误。网上有很多教程可以让你开始学习。这些日志语句显示它们已启用。我从代码片段中省略了它们,使事情变得简短明了:Log.dTAG,启用gps提供程序:+locationManager.isProviderEnabledLocationManager.gps\U提供程序;Log.dTAG,启用网络提供程序:+locationManager.isProviderEnabledLocationManager.network\u提供程序;谢谢Miten。以下是一些日志语句:mitenm@pinkydebian:/media/sf_E_DRIVE/tmp$adb logcat LocationNotifierService:*:S-----开始于/dev/log/system------开始于/dev/log/main I/LocationNotifierService 30866:null lastnownlocation D/LocationNotifierService 30866:gps提供程序启用:真D/LocationNotifierService 30866:network provider启用:真D/LocationNotifierService 30866:onCreate D/LocationNotifierService 30866:请求的位置更新D/LocationNotifierService 30866:请求的位置更新D/LocationNotifierService 30866:请求的位置更新