34

I got the Screen Size by using the following from a activity,

Display display = getWindowManager().getDefaultDisplay();

But the same thing is not working from a service (for obvious reasons, i know!) but i desperately need to get the display size from a service. Can some1 please explain me any other way for getting the ScreenSize??

So i guess as far as i see, there is no way we can get the Screen size from the service. Wat i have done as of now is to start the activity atleast 1ce by default and store the actual screen size in pixels in the shared preferences. I use the shared preferences to get the value when the service starts.

Is this the only way it'll work out??? Cant we by any means get the screen size from the service??

tshepang
  • 12,111
  • 21
  • 91
  • 136
JaVadid
  • 7,107
  • 13
  • 42
  • 54
  • 2
    check this:http://stackoverflow.com/questions/3166501/getting-the-screen-density-programmatically-in-android – Praveen Jul 09 '10 at 11:32
  • hi Praveen thanx for your reply, but as i explained above, the getWindowsManager() is a function which needs activity as context! and i m trying to do the same from a service! The above link doesnt help in getting the display size from a service. And in addition to that your question is about density while i need the pixels... – JaVadid Jul 10 '10 at 05:36
  • both are in my links itself. I have to tell you that Height and width are pixels nos. check this answer of my question: http://stackoverflow.com/questions/3166501/getting-the-screen-density-programmatically-in-android/3166551#3166551 – Praveen Jul 12 '10 at 09:43
  • Thanx again dude!!! But i guess u didnt read my above comment fully, as i have mentioned in my comment, the "getWindowManager().getDefaultDisplay().getMetrics(dm);" function can only be called from an activity, try calling it from a service, it doesn't work! It needs Activity as context! Hope you get my point! – JaVadid Jul 14 '10 at 04:15
  • 2
    I haven't tried it, but just from browsing through the docs, it looks like you can get the window manager via `Service.getSystemService(WINDOW_SERVICE);` – Tyler Jul 20 '10 at 06:18
  • sorry MatrixFrog it didnt work out either... Guess its not going to work this way... – JaVadid Aug 10 '10 at 04:15

4 Answers4

81

there totally is a way to obtain it from a service:

    WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
    Display display = window.getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
RJ M
  • 1,324
  • 12
  • 8
  • +1 for this good and detailed answer. I was looking for how to call `Display.getRotation()` from within a Service. Thanks a lot. – eternay Oct 23 '12 at 11:54
18

It worked for me!

DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

You only needed to get the context.

Mane
  • 181
  • 1
  • 2
0

If you are starting the service on boot (or automatically any other way) then shared prefs is a good way. However, if the only way you start the service is thru calling Context.startService() then it actually makes more sense to just place an extra in the Intent that you use to start the service.

Intrications
  • 16,782
  • 9
  • 50
  • 50
Hamy
  • 20,662
  • 15
  • 74
  • 102
  • Well Thanx a lot Hamy, but the service is more of a scheduled kind... i Don decide when it starts, the user decides the timing... Hence cant pass the information in intents... Thanx for your reply buddy... was dying to hear from some1 other than myself about ideas i can implement! – JaVadid Aug 10 '10 at 12:19
  • secondly correct me if i am wrong but i have heard that in case of services, the intent extras cannot be retrieved... don kno whether its true coz never tried it... – JaVadid Aug 10 '10 at 12:20
-1

Try this:

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

DisplayMetrics displayMetrics = new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(displayMetrics);

int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
prashant17
  • 1,520
  • 3
  • 14
  • 23