LocationManager loctionManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
loctionManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 0, locationListener);//监听位置变化,20秒一次,距离0米以上
private final LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
if (location != null)
{
Log.d("onLocationChanged", location.getLatitude()+","+location.getLongitude());
}
}
public void onProviderDisabled(String provider)
{
Log.d("onProviderDisabled", provider);
}
public void onProviderEnabled(String provider)
{
Log.d("onProviderEnabled", provider);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d("onStatusChanged", provider+",status="+status+",extras="+extras);
}
};
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><!-- 使用GPS权限 -->
注意:不用后一定要做清理工作:
loctionManager.removeUpdates(locationListener);