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

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 に答える
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 に答える