2012-05-04 3 views
0

이 작업을 수행 할 수 없습니다. 문제는 리스너가 위치 검색을 완료했다는 스레드에 절대로 알리지 않습니다.Thread and Loop.prepare() issue

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() 추가, 그것은 run 메서드를 만드는 끝난 후 rigth입니다. 답변 해 주셔서 감사합니다. – 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

@Nate, 처리기에 대한 참조가 없지만 대기중인/알림을 보내고있는 동일한 모니터에서 동기화해야하거나 동일한 결과로 끝나야합니다. 궁극적으로 OP는 'this'또는 'handler'참조에서 동기화해야하지만 두 번 모두 동일한 작업이 필요합니다. –