我正在尝试使用 getSystemService() 方法获取 WindowManager。这是我的代码:
private int calculateZoomLevel() {
int ht, screenWidth;
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager winManager;
winManager = Context.getSystemService(Context.WINDOW_SERVICE);
winManager.getDefaultDisplay().getMetrics(displayMetrics);
ht = displayMetrics.heightPixels;
screenWidth = displayMetrics.widthPixels;
double equatorLength = 40075004;
double widthInPixels = screenWidth;
double metersPerPixel = equatorLength / 256;
int zoomLevel = 1;
while ((metersPerPixel * widthInPixels) > 1000) {
metersPerPixel /= 2;
++zoomLevel;
}
Log.i(TAG, "zoom level is: "+ zoomLevel);
return zoomLevel;
}
具有以下代码的行:
winManager = Context.getSystemService(Context.WINDOW_SERVICE);
我得到的错误是:类型不匹配:无法从 Object 转换为 WindowManager
这段代码不是假设返回类型 WindowManager 吗?如何解决此问题,以便获得 WindowManager 的返回类型?