2012-05-04 8 views
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(); 
    } 

} 
+0

このスレッドをどこから始めますか? –

+0

Bharat Sharmaさん、私の質問を編集して、thread.start()を追加しました。 ありがとうございました。 – jcmonteiro

+0

スレッドを開始するのを忘れてしまった。 –

答えて

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

} 
0

問題は、同期ブロックからnotify()を呼び出していないです。元のソリューションを試してみたい場合は、これを試してみてください

private class GeoLocationHandler implements LocationListener { 

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

} 
+0

'ハンドラ'への参照がないので、代わりに 'this'を同期してください。 'notify()'の代わりに 'notifyAll()'を使うこともお勧めします。 – Nate

+0

@ネイト、ハンドラへの参照を持っていないのですが、あなたはwait/notifyを呼び出しているのと同じモニタで同期する必要があります。そうしないと、同じ結果が得られません。最終的にOPは 'this'または' handler'リファレンスで同期する必要がありますが、どちらも同じことが必要です –