2011-09-19 5 views
0

네트워크 공급자로부터 내 위치를 찾기 위해 GetURL 메서드를 정의 했으므로 아래 코드를 보낼 수 있습니다.GetLocation의 유형에 대해 getSystemService가 정의되지 않았습니다.

이 코드 조각을 내 기본 클래스의 Activity로 정의하면이 코드는 훌륭하게 작동하지만 별도의 클래스 (예 : GetLocation 클래스)를 만들 때 getSystemService 메서드를 사용할 수없고 제목에 오류가 발생합니다 (getSystemService is GetLocation의 유형에 대해 정의되지 않음). 이 항목에 관한 몇 가지 항목이 있지만 완전히 이해하지 못합니다.

신참 질문입니다. 응답 중에 이것을 고려하십시오. 감사합니다. Context 클래스에 속하는 getSystemService()

public String GetURL() { 
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    String locationProvider = LocationManager.NETWORK_PROVIDER; 
    mostRecentLocation = locationManager.getLastKnownLocation(locationProvider); 
    if(mostRecentLocation != null) { 
     double lat = mostRecentLocation.getLatitude(); 
     double lng = mostRecentLocation.getLongitude(); 
     currentLocation = "Lat: " + lat + " Lng: " + lng; 
     Log.e("LOCATION -------------", currentLocation + ""); 
    } 
    return currentLocation; 
} 

답변

5

방법.

따라서 다른 곳 유틸리티 방법이 될 getUrl()을 이동하려는 경우, 당신은 (그것을 Context에서 상속 등) 등 현재 Activity 같은 Context 객체를 전달해야합니다. 예를 들어

:
Util.java

public static String getUrl(Context context) { 
    LocationManager lm = (LocationManager) 
     context.getSystemService(Context.LOCATION_SERVICE); 

    // ... your existing code ... 
    return currentLocation; 
} 

MyActivity.java

public void onCreate() { 
    // ... usual stuff ... 

    String url = getUrl(this); 
} 
+0

감사합니다 크리스토퍼. 대신에 getBaseContext를 사용하여 메소드를 호출 했으므로 멋지게 작동했습니다. – DuyguK

관련 문제