Android从服务获取屏幕尺寸 [英] Android Get Screen dimensions from Service

查看:87
本文介绍了Android从服务获取屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过活动中的以下内容获得了屏幕尺寸,

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

Display display = getWindowManager().getDefaultDisplay();

但是同一件事不能通过服务工作(出于明显的原因,我知道!),但是我迫切需要从服务中获取显示尺寸.有人可以请我以其他任何方式来获取ScreenSize吗?

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??

因此,据我所知,我们无法从服务中获取屏幕尺寸.我到目前为止所做的事情是,默认情况下至少要启动1ce活动,并在共享首选项中以像素为单位存储实际屏幕大小.服务启动时,我使用共享的首选项获取值.

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??

推荐答案

完全可以通过一种方法从服务中获取它:

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();

这篇关于Android从服务获取屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆