5

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

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Brian
  • 7,955
  • 16
  • 66
  • 107

2 Answers2

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

Community
  • 1
  • 1
Hussain
  • 5,552
  • 4
  • 40
  • 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
  • 20,200
  • 11
  • 84
  • 98
kannappan
  • 2,250
  • 3
  • 25
  • 35