2012-07-10 3 views
1

현재 안드로이드에 대한 내 활동에 다음과 같은 코딩이 있습니다. 지금은 이것을 객체로 사용하고 위치 스캔을 시작하고 핸들러를 사용하여 3 초 후에 returnBestLocation 메서드를 사용하여 위치를 가져옵니다.사용자 정의 Android LocationListener 질문

그러나 나는 거기에 retrtive 위치 3s 나중에 개체를 호출하는 대신 MyLocationListener 개체가 자동으로 위치 변경 활동 호출 반환 가능성을 묻고 싶습니다?

public class MyLocationListener implements LocationListener { 

    LocationManager locationManager; 
    Date currentBestLocationDate; 
    Intent notificationIntent; 
    Context mContext; 
    Location currentBestLocation = null, lastKnownLocation=null; 

public MyLocationListener(Context mContext) 
{this.mContext = mContext; 

} 


public void startLocationScan() 
{ 
    Log.d(Config.log_id, "Custom Location Listener started"); 

    if (locationManager == null) { 

     locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 
     Location locationNETWORK = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     if (locationNETWORK != null) { 
      lastKnownLocation=locationNETWORK; 
     } 
     Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (locationGPS != null) { 
      lastKnownLocation=locationGPS; 

     } 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, MyLocationListener.this); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,MyLocationListener.this); 





    } 


} 
public void stopLocationScan() 
{ 

     if(locationManager!=null) 
     { 
      locationManager.removeUpdates(MyLocationListener.this); 

      Log.d(Config.log_id, "Custom Location Listener Stopped"); 
     } 
     } 
public Location returnBestLocation() 
{ 
    return currentBestLocation; 
} 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 


    @Override 
    public void onLocationChanged(Location location) { 


     // TODO Auto-generated method stub 
     if (currentBestLocation == null) { 
      currentBestLocation = location; 
     } 

     long timeDelta = location.getTime() - currentBestLocation.getTime(); 
     Log.d(Config.log_id, "locationpostingservice's changed with accuracy " + location.getAccuracy() + " s different " + (float) timeDelta/1000); 

     if (timeDelta >= 120000) { 
      currentBestLocation = location; 
      Log.d(Config.log_id,"posting service Location changed due to over 2min "+ location.getAccuracy() + " s different "+ (float) timeDelta/1000); 

     } 


     if (currentBestLocation.getAccuracy() >= location.getAccuracy()) { 
      currentBestLocation = location; 
     } 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

답변

4

알림을 받으려는 활동을 MyLocationListener에 리스너로 전달할 수 있습니다. 나만의 Listener 인터페이스를 만들고 Activity가이를 구현하고 addListener()와 같은 MyLocationListener에 메서드를 추가하도록합니다. 이러한 액티비티에 알리고 싶을 때마다 리스너 목록을 반복하고 locationChanged 메소드 (또는 인터페이스 정의에서 호출 한 이름)를 호출합니다. null 리스너 활동 등에 대한 오류 처리를 추가하십시오.

기본적으로 LocationListener를 수신하는 사용자 정의 리스너가 있습니다.

또 다른 방법은 브로드 캐스트 수신기를 사용하고 위치 변경을 브로드 캐스트하는 것입니다.

+0

안녕하세요, 자체 리스너 인터페이스 생성을 알려주시겠습니까? – ericlee

+0

공용 인터페이스 MyCustomListener {public void onLocationChanged (Location loc); } – Fraggle

+0

그러나 인터페이스를 만들면 LocationListener – ericlee

0

적절한 방법은 Fraggle이 말한 것처럼 독자적인 수신기 인터페이스를 만들어 클래스에 구현 한 다음 활동에서 수신기를 호출하는 것입니다.

그러나 빠른 n-dirty 대안은 새 클래스 (MyLocationListener)로 회전하지 않고 Activity에서 LocationListener 인터페이스를 구현하는 것일 수 있습니다. 그런 다음 실행하려는 모든 코드를 onLocationChanged에두면 다른 객체와 통신하는 것에 대해 걱정하지 않아도됩니다.

+0

을 구현할 수 없지만 리스너 인터페이스를 생성하여 리스너 인터페이스가 android locationlisterner를 구현할 수 없습니다. 다음은 실전 구현에 충실합니다. 나는 코드를 더 읽기 쉽게하려고 노력하고있다. – ericlee