Zapobiegaj rotacji ekranu na Androidzie

Mam jedną z moich czynności, której chciałbym zapobiec, ponieważ uruchamiam Asynktask, a obrót ekranu powoduje jego ponowne uruchomienie.

Czy istnieje sposób na powiedzenie tej czynności "nie obracaj ekranu, nawet jeśli użytkownik potrząsa telefonem jak szalony"?

Author: Peter Mortensen, 2010-04-28

13 answers

Dodaj

android:screenOrientation="portrait" 

Lub

 android:screenOrientation="landscape" 

Do <activity> elementu / elementów w manifest i jesteś skończony.

 406
Author: lbedogni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-30 13:18:48

Możesz postępować zgodnie z poniższą logiką, aby zapobiec automatycznemu obracaniu ekranu , gdy Twój AsyncTask jest uruchomiony:

  1. zapisz bieżącą orientację ekranu w aktywności za pomocą getRequestedOrientation().
  2. Wyłącz automatyczną orientację ekranu za pomocą setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR).
  3. Uruchom / wykonaj swoje AsyncTask.
  4. na końcu AsyncTask Przywróć poprzedni stan orientacji za pomocą setRequestedOrientation(oldOrientation).

Należy pamiętać, że istnieje kilka sposobów dostępu Activity (który działa na wątku UI) właściwości wewnątrz AsyncTask. Możesz zaimplementować swoją AsyncTask jako klasę wewnętrzną lub możesz użyć wiadomości Handler to szturchnie Twoją klasę.

 124
Author: Emre Yazici,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-19 15:52:55

W pliku manifestu, dla każdej aktywności, którą chcesz zablokować obrót ekranu Dodaj: jeśli chcesz zablokować ją w trybie poziomym:

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

Lub jeśli chcesz zablokować go w trybie pionowym:

<activity
            ...
            ...
            android:screenOrientation="portrait">
 24
Author: Sara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-01 16:12:35

Najprostszym sposobem na to było umieszczenie

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Wewnątrz onkreatu, tuż po

setContentView(R.layout.activity_main);
Więc...
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
 22
Author: Paul Alexander,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-22 10:01:09

Aktywność.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>
 5
Author: Li Che,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-27 12:10:35

Zamiast iść na AndroidManifest, możesz po prostu zrobić to:

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

Jedyną wadą jest to, że wymaga API na poziomie 18 lub wyższym. Więc w zasadzie to jest końcówka włóczni.

 5
Author: Antuan Develle Claude,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-04 19:51:16

Następujący atrybut aktywności w AndroidManifest.xml to wszystko czego potrzebujesz:

android:configChanges="orientation"

Więc pełny węzeł aktywności będzie:

<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>
 2
Author: Ben,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-04 19:46:32

Dodaj:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        ...
        ...
        ...
}
 1
Author: Anas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-04 19:50:53

Jeśli używasz Android Developer Tools (ADT) i Eclipse możesz iść na AndroidManifest.XML -- > Application tab -- > przejdź w dół i wybierz swoją aktywność. Na koniec wybierz preferowaną orientację. Możesz wybrać jedną z wielu opcji.

 0
Author: Husam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-04 19:49:42

Musisz dodać następujący kod w manifeście.plik xml. Aktywność, dla której nie powinna się obracać, w tej aktywności dodaj ten element

android:screenOrientation="portrait"

Wtedy nie będzie się obracać.

 0
Author: Simon Chius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-22 09:59:36

Możesz spróbować w ten sposób

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

 0
Author: Muhaiminurabir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-24 07:08:12

Dodaj następujące do AndroidManifest.xml

[ aplikacja > src > główna > AndroidManifest.xml]

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

Przykład:

<?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>
 0
Author: andrewoodleyjr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-24 03:38:09

Użyj AsyncTaskLoader , aby chronić swoje dane, nawet jeśli aktywność się zmieni, zamiast używać AsyncTask jest to lepszy sposób na tworzenie aplikacji niż zapobieganie rotacji ekranu.

 0
Author: Bbake Waikhom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-29 02:01:14