检测屏幕方向从服务变化

时间:2011-08-05 05:35:14

标签: android service screen orientation

一个非常简单明了的问题......服务是否可以检测到屏幕方向的变化?如果是这样,怎么样?

2 个答案:

答案 0 :(得分:8)

此链接将回答您的问题:How do I use a service to monitor Orientation change in Android

您还可以创建一个BroadcastReceiver来监听Intent.ACTION_CONFIGURATION_CHANGED"android.intent.action.CONFIGURATION_CHANGED"

答案 1 :(得分:-3)

试试此代码

@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);

    ... 
}

在android清单文件中

<activity android:name="..."
    android:label="@string/app_name"
    android:screenOrientation="sensor"
    android:configChanges="orientation|keyboardHidden">