Detecting screen orientation change from service [duplicate]
A pretty simple and straightforward question... 开发者_运维知识库is it possible for a service to detect screen orientation changes? If so, how?
This link will answer your question: How do I use a service to monitor Orientation change in Android
You can also create a BroadcastReceiver
that listens for Intent.ACTION_CONFIGURATION_CHANGED
("android.intent.action.CONFIGURATION_CHANGED"
)
Try this code
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
Log.d("tag", "Portrait");
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.d("tag", "Landscape");
else
Log.w("tag", "other: " + orientation);
...
}
in android manifest file
<activity android:name="..."
android:label="@string/app_name"
android:screenOrientation="sensor"
android:configChanges="orientation|keyboardHidden">
精彩评论