Android 从服务获取屏幕尺寸
我通过使用活动中的以下内容获得了屏幕尺寸,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
完全有一种方法可以从服务中获取它:
there totally is a way to obtain it from a service:
这对我有用!
您只需要获取上下文即可。
It worked for me!
You only needed to get the context.
如果您在启动时启动服务(或以任何其他方式自动启动),那么共享首选项是一个好方法。但是,如果启动服务的唯一方法是通过调用 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.
由于
getDefaultDisplay()
现在已被弃用,因此可以使用 (kotlin):Since
getDefaultDisplay()
now is depricated, this works (kotlin):试试这个:
Try this: