5

A pretty simple and straightforward question... is it possible for a service to detect screen orientation changes? If so, how?

PeeHaa
  • 69,318
  • 57
  • 185
  • 258
Brian
  • 7,777
  • 15
  • 64
  • 104

2 Answers2

10

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")

Community
  • 1
  • 1
Hussain
  • 5,526
  • 4
  • 39
  • 50
-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
  • 19,939
  • 11
  • 82
  • 96
kannappan
  • 2,264
  • 2
  • 25
  • 35
  • This solution works in Accessibility service. You can override onConfigurationChanged() and detect changes in the service. – Kumar Feb 01 '22 at 17:55