Reputation: 19966
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.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?
Upvotes: 3
Views: 10728
Reputation: 2971
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);
Upvotes: 0
Reputation: 752
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.
Upvotes: 15
Reputation: 19966
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.
Upvotes: 3
Reputation: 6866
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.
Upvotes: 7
Reputation: 803
Change the AndroidManifest.xml file. Thats enough.
<activity android:name="Activity_Name" android:screenOrientation="portrait"/>
Now your app will not change the Orientation.
Upvotes: 4