簡體   English   中英

Android位置管理器編譯錯誤

[英]Android location manager compilation error

我正在嘗試檢索LocationManager類的實例(獲取一些GPS相關信息)。 我曾經用過寫過一個簡單的類來做到這一點,但它最終給了我一個錯誤

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;
    }
}

謝謝。

錯誤消息表示您正在使用類( Context )進行需要類實例的調用。

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

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

如果您正在使用Activity中的LocationManagerHelper ,那么您可以將Activity作為上下文傳遞:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM