2011-02-04 82 views
3

我試圖檢索一個LocationManager類的實例(獲取一些GPS相關信息)。我用寫一個簡單的類的話,但最終給我一個錯誤Android位置管理器編譯錯誤

Cannot make a static reference to the non-static method getSystemService(String) from the type Context 

這裏是我的類

public class LocationManagerHelper { 

    static Location location = null; 

    public static Location getLocation() { 
     LocationManager manager = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE); 

     if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     } else { 
      System.out.println("Provider is disabled"); 
     } 
     return location; 
    } 
} 

感謝。

回答

10

錯誤消息表示您正在使用某個類(Context)撥打電話,需要一個類實例

您需要將Context實例傳遞到getLocation,並使用該Context實例調用getSystemService

public static Location getLocation (Context context) { 
    LocationManager manager = 
     (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    //.... 

如果您使用LocationManagerHelper從活動,那麼你可以通過活動的背景下:

LocationManagerHelper.getLocation(this); // "this" being an Activity instance 
+0

所以檢查一下這方面的工作,我需要我的電話主動GPS和Wi兩-Fi。到目前爲止沒有工作。 – Switch 2011-02-04 06:17:07