2013-12-20 41 views
0

我用下面的方法来确定当前的方位获取设备的方向不知道上下文

public boolean isPortrait() { 
    int orientation = mContext.getResources().getConfiguration().orientation; 
    return orientation == Configuration.ORIENTATION_PORTRAIT; 
    } 

现在我想搬到这个方法来单独的类,并使其静态能够与下面的语法进行访问Orientation.isPortait。问题是我需要用来确定方向的Context。如果没有Context,Activity等,有没有什么办法可以获得设备定位?

UPDATE:

所有我需要的是简化了我的代码,并把它缩短了。这就是为什么我根本不想使用或通过Context

+0

好吧,你希望是不同的。我们大多数人,当我们没有上下文并且需要时,我们会在类构造器中提供它。 android SDK正在不断地做它 – ramaral

回答

0

传递上下文到isPortrait():

public boolean isPortrait(final Context context) { 
    int orientation = context.getResources().getConfiguration().orientation; 
    return orientation == Configuration.ORIENTATION_PORTRAIT; 
} 

然后,只需通过上下文isPortrait():

Orientation.isPortrait(this); //this or whatever context you have 
+0

Thx,但它不适合我。有些我的课程根本没有上下文。 – user1248568