2011-02-05 9 views

risposta

45

Questo dovrebbe fare il trucco per voi:

import android.provider.Settings; 
    public static void setAutoOrientationEnabled(Context context, boolean enabled) 
    { 
      Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); 
    } 

Aggiungere il permesso al AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

È possibile trovare la documentazione here

+2

Questo sembra funzionare, ma si blocca che blocca l'orientamento in orizzontale per me anche se sono in verticale. Sto testando un tablet a nido d'ape quindi non sono sicuro se c'è una differenza .... È questo il comportamento previsto? – christoff

+0

Può essere fatto buy config solo, senza il permesso? Sto cercando di trovare risposta per questo https://stackoverflow.com/questions/44196296/application-enables-orientation-change-button-is-status-bar. –

2

sempre usare utente specificato orientamento dello schermo, questo si applicherà indipendentemente dall'orientamento scelto dall'utente e non ruoterà lo schermo se disabilitato.

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); 
1

Questa è la mia impementazione per questo problema. Ho dovuto implementare un pulsante che aveva la stessa funzione del pulsante di blocco dal menu delle impostazioni.

è possibile utilizzare i setRotationScreenFromSettings per risolvere il problema

public static boolean getRotationScreenFromSettingsIsEnabled(Context context) 
{ 

    int result = 0; 
    try 
    { 
     result = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION); 
    } 
    catch (Settings.SettingNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 

    return result == 1; 
} 



public static void setRotationScreenFromSettings(Context context, boolean enabled) 
{ 
    try 
    { 
     if (Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) == 1) 
     { 
      Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
      Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); 
      Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0); 
     } 
     else 
     { 
      Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1); 
     } 

     Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); 

    } 
    catch (Settings.SettingNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
} 

private void regirsterLockScreenOrientationChangedListner() 
{ 
    ContentObserver rotationObserver = new ContentObserver(new Handler()) 
    { 
     @Override 
     public void onChange(boolean selfChange) 
     { 
      refreshLockScreenOrientationBtn(); 
     } 
    }; 

    context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), 
      true, rotationObserver); 
}` 

Aggiungi autorizzazione al AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
1

Bisogna risolvere il problema utilizzando seguente codice all'interno onCreate) Funzione (. Lavorando ...

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
Problemi correlati