Android 保持应用启动时的方向

Android keep Orientation from app Start

是否可以根据应用程序启动时使用的方向来设置应用程序的方向?

例子

我的意思是说用户在 Landscape 中启动了我的应用程序。 O 希望它保存该状态并且不要让用户通过转动 phone 来更改方向(我想锁定它,直到用户重新运行应用程序并从那里应用程序再次检查用户是否以横向启动它或肖像 )

代码(到目前为止)

Configuration newConfig = getResources().getConfiguration();
  if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
             Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
             // set background for landscape through app
     } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
           // set background for portrait through app
  }

这是我在 SO 上找到的东西。我可以这样做吗?把它放在我的第一个 activity 中,然后使用这里的方向状态进行其余的活动。

可以使用ActivityInfo实现class。

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

检查 ActivityInfo here

Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);

buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   }

});

buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
   }

});

http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html

如果有人想获得有意义的方向描述(比如通过 reverseLandscape、sensorLandscape 等传递给 onConfigurationChanged(..) 的描述),只需使用 getRequestedOrientation()

您可以使用 SharedPreferences 存储一个值,即使应用程序关闭也会保存该值。
只需制作一个 class SaveOrientation :

public class SaveOrientation {

    static final String ORIENTATION = "orientation";

    static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    // value will be between 0 and 1
    public static void setOrientation(Context ctx, int value) {
        SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
        editor.putInt(ORIENTATION, value);
        editor.commit();
    }

    public static String getOrientation(Context ctx) {
        return getSharedPreferences(ctx).getString(ORIENTATION, "");
    }

}

然后在您的 MainActivity 中:

int portrait = 0;
int landscape = 1;
.
.
.
// in your onCreate :
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         SaveOrientation.setOrientation(this,portrait);
 }else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
         SaveOrientation.setOrientation(this,landscape);
}
applyOrientation();
.
.
.
// method applyOrientation
private void applyOrientation(){
   int orientation = SaveOrientation.getOrientation(this);

   if(orientation == portrait){
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

   }else if (orientation == landscape){
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}