Android 从服务获取屏幕尺寸

发布于 2024-09-08 14:42:40 字数 332 浏览 3 评论 0原文

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

Display display = getWindowManager().getDefaultDisplay();

但同样的事情在服务中不起作用(出于明显的原因,我知道!),但我迫切需要从服务中获取显示尺寸。有人可以向我解释一下获取屏幕尺寸的其他方法吗?

所以我想据我所知,我们无法从服务中获取屏幕尺寸。我现在所做的就是默认启动至少 1ce 的活动,并将实际屏幕尺寸(以像素为单位)存储在共享首选项中。我使用共享首选项在服务启动时获取值。

这是唯一的办法吗???我们不能通过任何方式从服务中获取屏幕尺寸吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笔落惊风雨 2024-09-15 14:42:40

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

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

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();
陪你搞怪i 2024-09-15 14:42:40

这对我有用!

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

您只需要获取上下文即可。

It worked for me!

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

You only needed to get the context.

写下不归期 2024-09-15 14:42:40

如果您在启动时启动服务(或以任何其他方式自动启动),那么共享首选项是一个好方法。但是,如果启动服务的唯一方法是通过调用 Context.startService(),那么在用于启动服务的 Intent 中放置一个额外的内容实际上更有意义。

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.

土豪 2024-09-15 14:42:40

由于 getDefaultDisplay() 现在已被弃用,因此可以使用 (kotlin):

val window = getSystemService(WINDOW_SERVICE) as WindowManager
val width: Int = window.currentWindowMetrics.bounds.height()
val height: Int = window.currentWindowMetrics.bounds.width()

Since getDefaultDisplay() now is depricated, this works (kotlin):

val window = getSystemService(WINDOW_SERVICE) as WindowManager
val width: Int = window.currentWindowMetrics.bounds.height()
val height: Int = window.currentWindowMetrics.bounds.width()
以为你会在 2024-09-15 14:42:40

试试这个:

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

DisplayMetrics displayMetrics = new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(displayMetrics);

int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

Try this:

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

DisplayMetrics displayMetrics = new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(displayMetrics);

int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文