开发者

Disable screen rotation at runtime on Android

public class testScreenRotation extends Activity {
/** Called when the activity is first created. */

private int mRuntimeOrientation;
   private boolean mDisableScreenRotation=true;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mRuntimeOrientation = this.getScreenOrientation();
    setContentView(R.layout.main);


}
protected int getScreenOrientation() {
/*
    Display display = getWindowManager().getDefaultDisplay();
    int orientation = display开发者_StackOverflow中文版.getOrientation();

    if (orientation == Configuration.ORIENTATION_UNDEFINED) {
       orientation = getResources().getConfiguration().orientation;

       if (orientation == Configuration.ORIENTATION_UNDEFINED) {
          if (display.getWidth() == display.getHeight())
             orientation = Configuration.ORIENTATION_SQUARE;
          else if(display.getWidth() < display.getHeight())

             orientation = Configuration.ORIENTATION_PORTRAIT;
          else
             orientation = Configuration.ORIENTATION_LANDSCAPE;
          }
       }


    return orientation;
  */
    return Configuration.ORIENTATION_PORTRAIT;
 }
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
       if (mDisableScreenRotation) {
             super.onConfigurationChanged(newConfig);
             this.setRequestedOrientation(mRuntimeOrientation);
          } else {
             mRuntimeOrientation = this.getScreenOrientation();
             super.onConfigurationChanged(newConfig);
          }
       }

}

my app as above ,and add android:configChanges="orientation" in the xml.when my app start my screen is PORTRAIT,i press ctrl+F12,the screen also roate,the screen is LANDSCAPE,second i press ctrl+F12,the screen also roate,the screen is PORTRAIT,then i press ctrl+F12,the screen keep PORTRAIT. so i press again,the screen keep PORTRAIT. my question is that why my screen not keep PORTRAIT when the app start.

Edit:i want to use code to control the screen roate, if i can do this?


If you are trying to get your application to be in Portrait-mode all the time, you can this line to the element in the manifest

android:screenOrientation="portrait"

Add This line in each activity in the AndroidManifest.xml file.


I believe the method you are looking for is Activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_<PORTRAIT/LANDSCAPE>)

This together with

android:configChanges="orientation"

for that activity in the manifest to tell the system you will handle the orientation yourself should disable automatic reorientation.


Change the AndroidManifest.xml file. Thats enough.

<activity android:name="Activity_Name" android:screenOrientation="portrait"/>

Now your app will not change the Orientation.


http://androidbiancheng.blogspot.com/2010/10/java-setrequestedorientation.html, this link solved my question, setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);and not need add the line android:configChanges="orientation" in xml.so i think i cannot use Configuration to get the screen Orientation,should use ActivityInfo.


You can disable screen rotation on runtime from java source code as well.
Just try this way

private void setAutoRotation(final boolean autorotate) {
        AsyncTask.execute(new Runnable() {
            public void run() {
                try {
                    IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));
                    if (autorotate) {
                        wm.thawRotation();
                    } else {
                        wm.freezeRotation(-1);
                    }
                } catch (RemoteException exc) {
                    Log.secW(TAG, "Unable to save auto-rotate setting");
                }
            }
        });
    }

Now try to call this method during your application runtime. May be in some methods or on some buttons.

setAutoRotation(false);

setAutoRotation(true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜