마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

service에서 orientation 체크하는 방법

0 추천

앱을 portrait mode 일때만 실행하고 싶고, 실행 후에도 portrait 전용으로 사용하고 싶어요.

 

1. Service에서 orientation을 체크해서 activity를 start 하려고 하려고 하는데,  

getResources().getConfiguration().orientation 는 activity에서만 사용 가능한가요?

service에서는 항상 0 값으로 들어와서 확인이 불가능하네요. 

 

2. menifest에 android:screenOrientation="portrait" 를 추가했더니, 

activity에서도 getResources().getConfiguration().orientation 값이 항상 0으로 들어와요.

 

서비스에서 orientation을 체크할 수 있는 방법과, 동시에 activity 실행후에도 portrait 전용으로 사용하는 방법 좀 알려주세요.. 

익명사용자 님이 2013년 11월 4일 질문
sensor로 체크하면 되네요.

1개의 답변

–1 추천

구하는 타이밍 문제일 듯 한데요.. 

아래를 참조하여 onConfigurationChanged 를 재구현해서 비교 해보세요. 
 
private Configuration oldConfig;
 
public void onCreate()
{
   super.onCreate();
   oldConfig = getResources().getConfiguration();;
}
 
public void onConfigurationChanged(Configuration newConfig)
{
   super.onConfigurationChanged(newConfig);
   if(newConfig.orientation != oldConfig.orientation)
   {
      // 회전 감지 
   }
   oldConfig = newConfig;
}

http://developer.android.com/reference/android/app/Service.html#onConfigurationChanged(android.content.res.Configuration)

사악미소 (65,330 포인트) 님이 2013년 11월 4일 답변
앱의 activity가 떠 있을 경우에는 onConfigurationChanged가 들어오지만, 아무것도 안떠있고,  service만 돌고 있을 때는 onConfigurationChanged를 타지 않습니다.....
...