Android 在 BroadcastReceiver 中使用 getWindowManager()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10955287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:33:17  来源:igfitidea点击:

using getWindowManager() inside BroadcastReceiver

androidbroadcastreceiver

提问by Ton

I am trying to use getWindowManager()inside BroadcastReceiverand I get this error:

我正在尝试在getWindowManager()内部使用BroadcastReceiver,但出现此错误:

"The method getWindowManager()is undefined for the type MyReceiver"

“该getWindowManager()类型的方法未定义MyReceiver

I just need to get display.getWidth()and display.getHeight()

我只需要得到display.getWidth()display.getHeight()

Any hints? Thanks a lot.

任何提示?非常感谢。

回答by Dawid Drozd

Simple code you only need context

简单的代码,你只需要上下文

        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);

More info go to documentation

更多信息请参阅文档

Or you can use this one

或者你可以使用这个

context.getResources().getDisplayMetrics()

but read documentation

但阅读文档

Return the current display metrics that are in effect for this resource object. The returned object should be treated as read-only.

返回对该资源对象有效的当前显示指标。返回的对象应被视为只读

回答by CommonsWare

getWindowManager()is a method on Activity. You cannot use this method. Please use getSystemService()to retrieve a WindowManager, as is described in the WindowManagerdocumentation.

getWindowManager()是一种方法Activity。您不能使用此方法。请使用getSystemService()检索 a WindowManager,如文档所述WindowManager