0

你好我已经实现了一个广播当前位置的服务。问题是它第一次更新它给了我 0.0 lat 和 0.0 lon,这破坏了 UI。我该如何解决这个问题?这是代码,谢谢

public class LocationService extends Service {
protected LocationManager locationManager;

final static String MY_ACTION = "MY_ACTION";
MyLocListener myloc = new MyLocListener();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override public void onDestroy() {

};
 @Override
public int onStartCommand(Intent intent, int flags, int startId){

 final Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(true);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);

LocationListener ll = new MyLocListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, ll);
Location location = new Location("abc");

ll.onLocationChanged(location);
Log.d("Last", ""+location.getLatitude());

return START_STICKY;
}

private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
Log.d("1provider",location.toString());
Log.d("1Provider LOCATION CHANGED", location.getLatitude() + "");
Log.d("1Provider LOCATION CHANGED", location.getLongitude() + "");



Intent intent = new Intent();
intent.putExtra("location", location);
intent.setAction(MY_ACTION);
sendBroadcast(intent);
    }

public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Log.d("1Provider DIsabled", "Provider Disabled");
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.d("1Provider Enabled", "Provider Enabled");
}

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.d("1Provider Changed", "Service Status Changed");
}
     }
   }
4

1 回答 1

0

onLocationChanged() 方法通常在第一次返回 0 - 只需检查此值而不更新 UI。至于时间太长,可能需要一段时间,因此在获取位置时将您的服务作为 AsyncTask 运行,以便您的 UI 线程等待,然后在您从 AsyncTask 获得返回时更新 UI。

于 2012-07-23T14:12:13.793 回答