没有活动的 getContext

发布于 2024-11-16 10:18:27 字数 856 浏览 2 评论 0原文

在启动任何活动之前是否可以获取设备宽高比?

我试图根据我的设备是否被识别为“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 技术交流群。

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

发布评论

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

评论(3

给妤﹃绝世温柔 2024-11-23 10:18:27

您的应用程序的应用程序上下文是在任何活动之前创建的。应用程序具有有效的上下文,因此您可以使用它来获取所需的任何内容,设置静态变量或成员变量,然后在第一个活动中引用它。

可以通过添加扩展应用程序的新类并在 AndroidManifest.xml 清单中进行一些小更改来处理应用程序

上下文:

<application android:enabled="true"...
   android:name=".Foo">
...
</application>

将新类添加到扩展应用程序的项目中:

public class Foo extends Application {

private int mWidth;

@Override
public void onCreate() {
    super.onCreate();
    // do stuff to set mWidth using the Application context like:
    // mWidth = getResources().getConfiguration().blahblahblah
}   

public int getWidth() {
    return mWidth;
}

从您的活动中,只需执行以下操作即可获取宽度:

int width = ((Foo)getApplication()).getWidth();

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:

<application android:enabled="true"...
   android:name=".Foo">
...
</application>

Add the new class to your project that extends Application:

public class Foo extends Application {

private int mWidth;

@Override
public void onCreate() {
    super.onCreate();
    // do stuff to set mWidth using the Application context like:
    // mWidth = getResources().getConfiguration().blahblahblah
}   

public int getWidth() {
    return mWidth;
}

From your activity, simply do this to get width:

int width = ((Foo)getApplication()).getWidth();
遮了一弯 2024-11-23 10:18:27

这是在哪里执行的?我不明白如何执行代码而不实例化从 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.

溺渁∝ 2024-11-23 10:18:27

当您从活动中调用此方法时。您可以将“this”作为 getDefaultWidth() 方法的参数传递。然后您可以将其用作上下文。

public int getDefaultWidth(Context context) { ... }

在活动中称之为

int width = getDefaultWidth(this)

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.

public int getDefaultWidth(Context context) { ... }

and in the activitym call it like

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