source

Android에서 화면 회전 방지

ittop 2023. 7. 26. 22:25
반응형

Android에서 화면 회전 방지

비동기 작업을 시작하고 화면을 회전하면 다시 시작되기 때문에 작업이 회전하지 않도록 방지하려는 작업이 있습니다.

이 활동에 대해 "사용자가 미친 듯이 휴대폰을 흔들고 있더라도 화면을 돌리지 마십시오"라고 말할 수 있는 방법이 있습니까?

더하다

android:screenOrientation="portrait" 

또는

 android:screenOrientation="landscape" 

에게<activity>매니페스트의 요소/s를 확인하면 완료됩니다.

아래의 논리에 따라 화면이 자동으로 회전하는 것을 방지할 수 있습니다.AsyncTask실행 중:

  1. 를 사용하여 현재 화면 방향을 활동 내부에 저장합니다.
  2. 를 사용하여 자동 화면 방향을 비활성화합니다.
  3. 실행/실행AsyncTask.
  4. 당신의 마지막에.AsyncTask를 사용하여 이전 오리엔테이션 상태를 복원합니다.

액세스할 수 있는 몇 가지 방법이 있습니다.Activity(UI 스레드에서 실행됨) 내부의 속성AsyncTask다음을 구현할 수 있습니다.AsyncTask내부 클래스로 또는 당신의 메시지를 찌르는 것을 사용할 수 있습니다.Activiy학급.

제가 찾은 가장 쉬운 방법은

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

매니페스트 파일에서 화면 회전을 잠그려는 각 활동에 대해 수평 모드로 잠그려면 다음을 추가합니다.

<activity
        ...
        ...
        android:screenOrientation="landscape">

또는 수직 모드에서 잠그려면 다음을 수행합니다.

<activity
            ...
            ...
            android:screenOrientation="portrait">

Android Manifest로 이동하는 대신 다음 작업을 수행할 수 있습니다.

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 이상이 필요하다는 것입니다.기본적으로 이것은 창 끝입니다.

활동.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) {       
     }   

Android Manifest.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>

사용자"portrait"AndroidManifest.xml 파일에서 좋은 해결책으로 보일 수 있습니다.그러나 특정 장치(가로에서 가장 잘 작동하는 장치)가 올바른 방향으로 이동하지 않고 세로로 이동하도록 강제합니다.최신 안드로이드 버전에서는 오류가 발생합니다.그래서 제 제안은 그것을 사용하는 것이 더 낫습니다."nosensor".

<activity
        ...
        ...
        android:screenOrientation="nosensor">

AndroidManifest.xml에 다음을 추가합니다.

[ app > src > main > AndroidManifest.xml ]

<activity android:name=".MainActivity"
          android:configChanges="orientation"
          android:screenOrientation="portrait"/>

예:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.zzzzzz.yyyyy">

   <uses-permission android:name="A-PERMISSION" />

   <application>
      <activity android:name=".MainActivity"
                android:screenOrientation="portrait"
                android:configChanges="orientation">
      </activity>
   </application>

</manifest>

AndroidManifest.xml의 활동에 대한 다음 속성만 있으면 됩니다.

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>

추가:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        ...
        ...
        ...
}

화면 회전 방지는 매니페스트에 다음 행을 추가하기만 하면 됩니다.

<activity
        android:name=".YourActivity"
        android:screenOrientation="portrait" />

이것은 나에게 효과가 있습니다.

Android:애플리케이션 태그 <애플리케이션>의 화면 방향="세로"

그리고.

<activity ... ... Android:화면 방향="잠김">

Android Developer Tools(ADT) Eclipse를 사용하는 경우 AndroidManifest.xml --> Application 탭 --> 아래로 내려가 활동을 선택할 수 있습니다.마지막으로 원하는 방향을 선택합니다.여러 옵션 중 하나를 선택할 수 있습니다.

다음 코드를 manifest.xml 파일에 추가해야 합니다.회전하지 않아야 하는 활동에서 이 요소를 추가합니다.

android:screenOrientation="portrait"

그러면 회전하지 않습니다.

이 방법을 사용해 보십시오.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itclanbd.spaceusers">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Login_Activity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

화면 회전을 방지하는 것보다 앱을 만드는 더 좋은 방법인 비동기 작업을 사용하는 대신 활동이 변경되더라도 데이터를 안전하게 유지하려면 비동기 작업 로더를 사용합니다.

언급URL : https://stackoverflow.com/questions/2730855/prevent-screen-rotation-on-android

반응형