0

我不能让它工作。问题是,线程永远不会被通知侦听器已完成搜索位置。

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();
    }

}
4

2 回答 2

0

想出了解决方案,不是按照我想要的方式安静,但它确实有效。将进度对话框创建为字段并通过处理程序将其关闭。

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.ibMap:
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setTitle("Procurando");
    mProgressDialog.setMessage("Localizando o dispositivo.");
    mProgressDialog.show();
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGeoHandler);
        break;
    default:
    Toast.makeText(getBaseContext(), "Nothing yet.", Toast.LENGTH_SHORT).show();
    }
}

private class GeoLocationHandler implements LocationListener {

    public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    mLocation = location;
    mHandler.sendEmptyMessage(0);
    }

}

private class MyHandler extends Handler {

@Override
public void dispatchMessage(Message msg) {
    mProgressDialog.dismiss();
    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=UFRJ,RJ"));
    startActivity(intent);
    super.dispatchMessage(msg);
}

}
于 2012-05-04T13:32:12.970 回答
0

问题是您不是notify()从同步块调用。如果您想尝试让您的原始解决方案工作,试试这个

private class GeoLocationHandler implements LocationListener {

    public void onLocationChanged(Location location) {
      // TODO Auto-generated method stub
      mLocation = location;
      synchronized(handler){
          notify();
      }
    }

}
于 2012-05-04T13:34:14.933 回答