我正在尝试检索LocationManager类的实例(获取一些GPS相关信息).我曾经用过写过一个简单的类来做到这一点,但它最终给了我一个错误
Cannot make a static reference to the non-static method getSystemService(String) from the type Context
Run Code Online (Sandbox Code Playgroud)
这是我的课
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;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
Dan*_*lau 10
错误消息表示您正在使用class(Context)进行需要类实例的调用.
您需要将Context实例传递给getLocation,并使用该Context实例进行调用getSystemService.
public static Location getLocation (Context context) {
LocationManager manager =
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//....
Run Code Online (Sandbox Code Playgroud)
如果您正在使用LocationManagerHelperActivity,那么您可以将Activity作为上下文传递:
LocationManagerHelper.getLocation(this); // "this" being an Activity instance
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3483 次 |
| 最近记录: |