0

我正在编写一个简单的服务来监听位置变化。在我的 onStart 方法中,我有以下内容

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationListener = new MyLocationListener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

然后在 onLocationChanged(Location loc) 方法中处理服务如何对更改做出反应。

但是我想在服务启动时获取初始位置。假设位置信息没有任何变化,我想在服务启动时至少使用一次位置信息,即使位置没有改变。怎么做?

4

1 回答 1

1

在这里使用getLastKnownLocation

以下代码段将为您提供帮助。

LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);
于 2012-06-05T12:04:12.347 回答