2016-06-29 2 views
1

일반 수업의 활동에서 위도 경도를 얻을 계획이었습니다.Android에서 Activity에서 참조 할 때 일반 클래스에서 Long, Long을 가져 오기 위해 onConnected() 메서드가 호출 될 때까지 기다리는 방법은 무엇입니까?

GPSResource gpsResource = new GPSResource(getApplicationContext()); 
    double lat = gpsResource.getLatitude(); 

항상 null을 반환합니다. onConnected()가 호출 될 때까지 기다렸다가 참조 된 활동에 값을 반환하도록 코드를 어떻게 수정해야합니까?

public class GPSResource implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener,LocationSource.OnLocationChangedListener,LocationListener 

{ 
    private Location location; // location 
    private double latitude; // latitude 
    private double longitude; // longitude 
    private GoogleApiClient mGAC; 
    private Context mContext; 
    public static final String TAG = "GPSresource"; 
    private LocationRequest mLocationRequest; 


public GPSResource(Context c) 
    { 
     mContext = c; 
     try 
     { 
      buildGoogleApiClient(); 
     } 
     catch(Exception e) 
     { 
      Log.d(TAG,e.toString()); 
     } 
    } 

    protected synchronized void buildGoogleApiClient() 
    { 
     mGAC = new GoogleApiClient.Builder(mContext) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 

     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_LOW_POWER) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 

     mGAC.connect(); 

    } 


    public double getLatitude(){ 

     return latitude; 
    } 

    /** 
    * Function to get longitude 
    * */ 
    public double getLongitude() { 
     if (location != null) { 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 


    @Override 
    public void onConnected(Bundle bundle) { 

     Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(mGAC); 
     if (currentLocation == null) 
     { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGAC, mLocationRequest, this); 
     } 

     latitude = currentLocation.getLatitude(); 
     longitude = currentLocation.getLongitude(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 


    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 


private boolean checkLocServices() 
{ 

    LocationManager manager = null; 
    boolean isAvailable = false; 

    try 
    { 
     manager = (LocationManager)mContext.getSystemService(mContext.LOCATION_SERVICE); 
     if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)||manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
     { 
      isAvailable = true; 
     } 
     else 
     { 
      isAvailable = false; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return isAvailable; 

} 


@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    if(location!=null) 
    { 
     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
    } 


} 

}

답변

1
  1. 다음과 같이 인터페이스 만들기 :

    public interface GPSResourceListener { 
        void onLocationUpdated(Location location); 
    } 
    
  2. 이 활동하고 패스 위의 인터페이스를 구현을 여기에

    는 일반 클래스에 대한 나의 코드입니다 다음과 같이 GPSResource 생성자에 대한 인스턴스 :

    ,210
    class YourActivity extends AppCompatActivity implements GPSResourceListener { 
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
         GPSResource gpsResource = new GPSResource(getApplicationContext(), this); 
        } 
    } 
    
  3. GPSResource 클래스에서 다음과 같이 변경합니다 :

    ... 
    public GPSResource(Context c, GPSResourceListener listener) { 
        mContext = c; 
        mListener = listener 
        ... 
    } 
    ... 
    @Override 
    public void onConnected(Bundle bundle) { 
        ... 
        mListener.onLocationUpdated(currentLocation); 
    } 
    ... 
    @Override 
    public void onLocationChanged(Location location) { 
        if(location!=null) { 
         mListener.onLocationUpdated(location); 
        } 
    }