Detecting screen orientation change from service [duplicate]

喜夏-厌秋 提交于 2019-12-07 06:07:30

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/6951874/detecting-screen-orientation-change-from-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!