using getWindowManager() inside BroadcastReceiver

Ton picture Ton · Jun 8, 2012 · Viewed 51.5k times · Source

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

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

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

Any hints? Thanks a lot.

Answer

Dawid Drozd picture Dawid Drozd · Aug 14, 2013

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.