AsyncTask를 시작하기 때문에 회전하지 못하게하려는 활동 중 하나가 있으며 화면 회전으로 다시 시작됩니다.
이 활동을 알리는 방법이 있습니까? “사용자가 화를 내면서 전화를 흔들어도 화면을 회전시키지 마십시오”?
답변
더하다
android:screenOrientation="portrait"
또는
android:screenOrientation="landscape"
<activity>
매니페스트 의 요소에 대한 작업이 완료되었습니다.
답변
당신은 자동 화면 회전을 방지하기 위해 아래의 논리를 따를 수 있는 동안 당신은 AsyncTask
실행이다 :
- 를 사용하여 현재 화면 방향을 활동 내에 저장하십시오
getRequestedOrientation()
. - 를 사용하여 자동 화면 방향을 비활성화하십시오
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)
. - 를 실행 / 실행하십시오
AsyncTask
. - 당신의 끝에서
AsyncTask
사용하여 이전 방향 상태를 복원합니다setRequestedOrientation(oldOrientation)
.
의 Activity
내부 (UI 스레드에서 실행) 속성에 액세스 하는 방법에는 여러 가지가 있습니다 AsyncTask
. AsyncTask
내부 클래스로 구현하거나 클래스 Handler
를 찌르는 메시지 를 사용할 수 있습니다 Activiy
.
답변
매니페스트 파일에서 화면 회전을 잠 그려는 각 활동에 대해 가로 모드로 잠 그려면 다음을 추가하십시오.
<activity
...
...
android:screenOrientation="landscape">
또는 세로 모드로 잠 그려면 다음을 수행하십시오.
<activity
...
...
android:screenOrientation="portrait">
답변
이 작업을 수행하는 가장 쉬운 방법은
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
바로 onCreate 내에서
setContentView(R.layout.activity_main);
그래서…
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
답변
AndroidManifest에 들어 가지 않고 다음과 같이 할 수 있습니다.
screenOrientation = getResources().getConfiguration().orientation;
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
... AsyncTask
screenOrientation = getResources().getConfiguration().orientation;
@Override
protected void onPostExecute(String things) {
context.setRequestedOrientation(PlayListFragment.screenOrientation);
or
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
여기서 유일한 단점은 API 레벨 18 이상이 필요하다는 것입니다. 기본적으로 이것은 스피어의 끝입니다.
답변
Activity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="QRCodeActivity" android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
답변
AndroidManifest.xml의 ACTIVITY에 대한 다음 속성 만 있으면됩니다.
android:configChanges="orientation"
따라서 전체 활동 노드는 다음과 같습니다.
<activity android:name="Activity1"
android:icon="@drawable/icon"
android:label="App Name"
android:excludeFromRecents="true"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>