0

I know about using getWindowManager().getDefaultDisplay().getMetrics(metrics); to grab screen size for an activity, but how can I go about getting the screen size for a service? Please let me know if I need to explain more.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ja Guz
  • 3
  • 5

2 Answers2

0

What does that mean Screen size for Service ?

Service is a background process of app, it does not have a UI. (thus no screensize) You can read more about Services at Android Documentation

Or if I am getting right , you want to access screen size from a service.

An hack will be to store screen size in preference when app starts from first activity. And then use it anywhere even when app is in background. Because screen size will not be changed.

Edit

After seeing your image, if it is a dialog, you can use below way to make your dialog full screen.

Dialog dialog=new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen)
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Here is an image of my app. I am using a service class to allow my floating ui to overlay. http://oi67.tinypic.com/2hhk3sk.jpg – Ja Guz Aug 10 '18 at 10:02
  • Sorry that wasn't what I was looking for hehe but I may have just worded it incorrectly. I did find my answer, but thank you anyways! – Ja Guz Aug 10 '18 at 10:07
  • What was the issue then, and how did you resolve that? – Khemraj Sharma Aug 10 '18 at 10:08
  • From inside a service class, I wanted to get the size of the screen. – Ja Guz Aug 10 '18 at 10:10
  • You could use `An hack` line solution – Khemraj Sharma Aug 10 '18 at 10:13
  • WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Display display = window.getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); This worked very well for me, so I will just follow through with this. Thanks again. – Ja Guz Aug 10 '18 at 10:14
0

Try this inside your service:

WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
Display display = window.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

OR

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
  • Thank you. I feel really silly for not trying to use getSystemService. I looked it up but couldn't find any use xD I have no coding background. I just research. Would you recommend anything I can read upon? – Ja Guz Aug 10 '18 at 10:06
  • What I didnt take into count was the notification bar. I only get the screen size of everything below the notification bar. Can you help me further? – Ja Guz Aug 10 '18 at 10:59
  • This might help: https://stackoverflow.com/questions/26674378/android-get-screen-size-including-the-size-of-status-bar-and-software-navigation – prashant17 Aug 10 '18 at 11:09
  • the solution from link should work. This is what get from service with code from my answer `setScreenSize: 1080--------1794` and this from Service with code from above link `setScreenSize: 1080--------1920` in my sample app – prashant17 Aug 10 '18 at 11:32
  • If the above solution worked for you, kindly accept the answer. – prashant17 Aug 13 '18 at 06:43