我需要使用计时器以特定间隔检查更新。检查当前位置。但是,我需要在获得用户的当前位置后启动计时器。因此,我需要手动启动计时器。有没有办法可以在此检查的基础上启动计时器的计划任务?
答案 0 :(得分:1)
您无法使用timer
获取位置更新。已经有办法获取用户当前位置的更新。
您应该调用requestLocationUpdates()
方法获取lastKnownLocation
的更新。
方法就像,
requestLocationUpdates(String networkProvider, int timeInMillis, int distanceInMeters, this);
String networkProvider
可以是LocationManager.NETWORK_PROVIDER
或LocationManager.GPS_PROVIDER
你应该使用它,
LocationManager mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 100, this);
这将在1000毫秒和100米之后获得最近一次的位置更新。
要获得纬度经度,
Location location;
if (mLocMgr != null) {
location = mLocMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
System.out.println(latitude+" "+longitude);
}
}
中的位置管理器