在android中手动启动计时器

时间:2015-03-22 09:00:02

标签: android timer

我需要使用计时器以特定间隔检查更新。检查当前位置。但是,我需要在获得用户的当前位置后启动计时器。因此,我需要手动启动计时器。有没有办法可以在此检查的基础上启动计时器的计划任务?

1 个答案:

答案 0 :(得分:1)

无法使用timer获取位置更新。已经有办法获取用户当前位置的更新。

您应该调用requestLocationUpdates()方法获取lastKnownLocation的更新。

方法就像,

requestLocationUpdates(String networkProvider, int timeInMillis, int distanceInMeters, this);

String networkProvider可以是LocationManager.NETWORK_PROVIDERLocationManager.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);
    }
}

请参阅documentation

中的位置管理器