我不能让它工作。问题是,线程永远不会被通知侦听器已完成搜索位置。
final ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Procurando");
progress.setMessage("Localizando o dispositivo.");
progress.show();
Thread thread = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
GeoLocationHandler handler = new GeoLocationHandler();
try {
synchronized (handler) {
Looper.prepare();
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,handler);
handler.wait();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
progress.dismiss();
mLocationManager.removeUpdates(handler);
double lat = mLocation.getLatitude();
double lng = mLocation.getLongitude();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?saddr=" + lat + "," + lng + "&daddr=-22.858114, -43.231295"));
startActivity(intent);
}
}
thread.start();
这是实现 LocationListener 的类:
private class GeoLocationHandler implements LocationListener {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
mLocation = location;
notify();
}
}