2011-08-01 8 views
7

화면 회전을 감지 할 수 있습니까? 내 말은 - 순환 게재인데, 이는 다른 활동의 활동 초기화와 명확하게 구별 할 수 있습니까?화면 회전을 어떻게 감지 할 수 있습니까?

XXX의 방법이 유용 할 않는 것

, 나는합니다 (제거가 회전에 플래그가 반영되지 않는 것)을 starting Intent에서 flag을 추가/제거 시도하고 안드로이드를 추가 시도했다 : configcheanges = "orientation"onConfigurationChanged 메쏘드의 액티비티를위한 메쏘드는 두번째 회전마다 호출되는 것처럼 보입니다.

내가 뭔가를 놓치고있는 것 같지만 ... 다른 관련 스레드에서 명확한 해결책을 찾지 못했습니다.

아이디어가 있으십니까?

+0

가로에서 세로로 또는 그 반대로 전환하는 경우를 의미합니까? – Pengume

+0

이 스레드가 같지 않습니까? : http://stackoverflow.com/questions/4843809/how-do-i-detect-screen-rotation – Viren

+0

onConfigurationChanged 메서드에서 화면 회전과 같이 발생한 ConfigurationChanged 유형을 감지 할 수 있습니다. – mikepenz

답변

11

매니페스트 :

<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/> 

활동 :

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

    .... 
} 

이 링크를 시도 또한

How do I detect screen rotation

+0

답장을 보내 주셔서 감사합니다. 그러나, 나는 질문을 게시하기 전에 그것을 시도하고, 이것은 나를 위해, 왜 그런지 모르겠지만 onConfigurationChanged 때마다 호출되지 않습니다. 오리엔테이션이 원본과 동일해질 때만 호출됩니다 (매초 두 번째). – MerlinBG

+0

어디에서 테스트하고 있습니까? 코드를 게시 할 수 있습니까? 왜냐하면 나는 그것을 전화로 테스트했기 때문에 작동했다. – Finuka

+0

나는 그것을 에뮬레이터에서 테스트하고 LogCat 뷰를보고있다. (만이 활동) 매니페스트 android:configChanges="orientation|keyboard" 및 활동 클래스 @Override \t public void onConfigurationChanged(Configuration newConfig) { \t \t super.onConfigurationChanged(newConfig); \t \t \t \t Log.d(" - ", "onConfigChanged"); // TODO remove \t } ... – MerlinBG

0

왜 당신이 시도하지? 방향이 변경된 경우, 을의 onConfigurationChanged 방법을 무시하고 확인 후

Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
myOrientation = display.getOrientation(); 

: 잊지 마세요

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    if(newConfig.orientation != myOrientation) 
     Log.v(tag, "rotated"); 
    super.onConfigurationChanged(newConfig); 
} 

에서

전화의 방향을 얻을 onCreated 액티비티의 매니페스트 android:configChanges="orientation"을 추가하십시오.

1

화면 회전을 감지하기 위해 onConfigurationChanged 메소드를 사용하는 것은 좋지 않습니다. 사용자가 화면을 회전 할 때이 활동의 ​​구성 변경이 올바르게 작동하지 않습니다.

그래서이 솔루션을 사용하여이 문제를 해결합니다.

public class SampleActivity extends AppCompatActivity { 
    public static final String KEY_LAST_ORIENTATION = "last_orientation"; 
    private int lastOrientation; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     if (savedInstanceState == null) { 
      lastOrientation = getResources().getConfiguration().orientation; 
     } 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     checkOrientationChanged(); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 
     lastOrientation = savedInstanceState.getInt(KEY_LAST_ORIENTATION); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putInt(KEY_LAST_ORIENTATION, lastOrientation); 
    } 

    private void checkOrientationChanged() { 
     int currentOrientation = getResources().getConfiguration().orientation; 
     if (currentOrientation != lastOrientation) { 
      onScreenOrientationChanged(currentOrientation); 
      lastOrientation = currentOrientation; 
     } 
    } 

    public void onScreenOrientationChanged(int currentOrientation) { 
     // Do something here when screen orientation changed 
    } 
} 

하지만 내 프로젝트에 사용할 수있을만큼 코드 여전히 좋지 않다, 그래서 난 내 자신의 라이브러리 (https://github.com/akexorcist/ScreenOrientationHelper)이 코드를 적용했다.

compile 'com.akexorcist:screenorientationhelper:<latest_version>' 

그런 다음 완료를이 기본 클래스

public class MainActivity extends BaseActivity { 

    ... 

    @Override 
    public void onScreenOrientationChanged(int orientation) { 
     // Do something when screen orientation changed 
    } 
} 

로 활동을 확장이

public class BaseActivity extends AppCompatActivity implements ScreenOrientationHelper.ScreenOrientationChangeListener { 
    private ScreenOrientationHelper helper = new ScreenOrientationHelper(this); 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     helper.onCreate(savedInstanceState); 
     helper.setScreenOrientationChangeListener(this); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     helper.onStart(); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     helper.onSaveInstanceState(outState); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 
     helper.onRestoreInstanceState(savedInstanceState); 
    } 

    @Override 
    public void onScreenOrientationChanged(int orientation) { 

    } 
} 

같은 기본 활동 클래스를 만들 수 있습니다!

관련 문제