My android app will run at background and I need to be notify immediately when orientation change. eg. When user rotate the phone from portrait to landscape (or landscape to portrait), the system should be tell my app that the orientation has changed.
Thanks.
This method is called when screen rotated
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Do stuff here
}
You do not have to explicitly set the Orientation listener.
onConfigurationChanged
will be called by the system.
@Override
public void onConfigurationChanged(Configuration myConfig) {
super.onConfigurationChanged(myConfig);
int orient = getResources().getConfiguration().orientation;
switch(orient) {
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
default:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With