由於某些原因,在requestLocationUpdates()方法之後添加removeUpdates()時,我根本沒有獲取任何位置數據。我沒有正確取消訂閱嗎?在獲取位置數據後從LocationManager中取消訂閱LocationListener
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 1, mlocListener);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, mlocListener);
mlocManager.removeUpdates(mlocListener);
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String text = "My current location is: " +
"Latitude = " + loc.getLatitude() +
"Longitude = " + loc.getLongitude();
System.out.println("FINISHED:");
Toast.makeText(getApplicationContext(),
text,
Toast.LENGTH_SHORT).show();
}
}
我見過的最髒的東西之一:( – FlorianB