没有活动的 getContext
在启动任何活动之前是否可以获取设备宽高比?
我试图根据我的设备是否被识别为“long”或“not_long”来设置宽度。这应该在活动启动之前很久就可用,但我不确定在哪里可以获得这个变量。执行此方法时,活动不可用。另外,为了避免内存泄漏,我不想将活动传递到包含以下方法的类中。
这是失败的调用方法:
public int getDefaultWidth() {
Configuration myConfig = new Configuration();
myConfig.setToDefaults();
myConfig = getResources().getConfiguration();
switch (myConfig.screenLayout & Configuration.SCREENLAYOUT_LONG_MASK) {
case Configuration.SCREENLAYOUT_LONG_NO: {
return this._defaultWidth;
}
case Configuration.SCREENLAYOUT_LONG_YES: {
return this._defaultWidthLong;
}
case Configuration.SCREENLAYOUT_LONG_UNDEFINED: {
return this._defaultWidth;
}
}
return this._defaultWidth;
}
Is it possible to get device aspect ratio before launching any activity?
I am trying to setup the width based on whether my device is identified as 'long' or 'not_long'. This should be available long before the activity is launched but I am not sure where I can get this variable. Activity is NOT available at the time this method is executed. Also in order to avoid memory leaks I do not want to pass activity into the class that contains the following method.
Here is the failing call method:
public int getDefaultWidth() {
Configuration myConfig = new Configuration();
myConfig.setToDefaults();
myConfig = getResources().getConfiguration();
switch (myConfig.screenLayout & Configuration.SCREENLAYOUT_LONG_MASK) {
case Configuration.SCREENLAYOUT_LONG_NO: {
return this._defaultWidth;
}
case Configuration.SCREENLAYOUT_LONG_YES: {
return this._defaultWidthLong;
}
case Configuration.SCREENLAYOUT_LONG_UNDEFINED: {
return this._defaultWidth;
}
}
return this._defaultWidth;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的应用程序的应用程序上下文是在任何活动之前创建的。应用程序具有有效的上下文,因此您可以使用它来获取所需的任何内容,设置静态变量或成员变量,然后在第一个活动中引用它。
可以通过添加扩展应用程序的新类并在 AndroidManifest.xml 清单中进行一些小更改来处理应用程序
上下文:
将新类添加到扩展应用程序的项目中:
从您的活动中,只需执行以下操作即可获取宽度:
Your app's Application context is created before any activities. An Application has a valid context, so you could use that to fetch whatever you need, set a static or member variable, and then reference that in your first activity.
An Application context can be handled by adding a new class that extends Application and making a small change in AndroidManifest.xml
Manifest:
Add the new class to your project that extends Application:
From your activity, simply do this to get width:
这是在哪里执行的?我不明白如何执行代码而不实例化从 Context 继承的东西。 Service继承自Context,BroadcastReceiver的onReceive被赋予一个Context。
此外,您可以只从调用此方法的位置传入一个 Configuration 对象,而不是传递 Context。
Where is this is executing? I don't see how you could be executing code and not have something instantiated that inherits from Context. Service inherits from Context and a BroadcastReceiver's onReceive is given a Context.
Also, instead of passing a Context in, you could just pass in a Configuration object from where ever this method is being called.
当您从活动中调用此方法时。您可以将“this”作为 getDefaultWidth() 方法的参数传递。然后您可以将其用作上下文。
在活动中称之为
When you call this method from an activity. you can pass "this" as a parameter for getDefaultWidth() method. Then you can use it as a context.
and in the activitym call it like