A pretty simple and straightforward question... is it possible for a service to detect screen orientation changes? If so, how?
Asked
Active
Viewed 1.0k times
2 Answers
11
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"
)
-4
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">

Sergey Glotov
- 20,200
- 11
- 84
- 98

kannappan
- 2,250
- 3
- 25
- 35
-
This solution works in Accessibility service. You can override onConfigurationChanged() and detect changes in the service. – SemicolonSpace Feb 01 '22 at 17:55