2012-10-17 16 views
5

Mi sono guardato intorno e ho potuto trovare solo il codice che imposterà la luminosità su quella attività. Sto cercando di cambiare le impostazioni effettive del telefono. Il codice che ho provato è:Android SDK: come si imposta la luminosità dello schermo

public class AutoPowerManagerActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     try { 
      adjustBright(); 
     } catch (SettingNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    private void adjustBright() throws SettingNotFoundException { 
     // TODO Auto-generated method stub 
     int brightnessMode = Settings.System.getInt(getContentResolver(), 
       Settings.System.SCREEN_BRIGHTNESS_MODE); 
     if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { 
      Settings.System.putInt(getContentResolver(), 
        Settings.System.SCREEN_BRIGHTNESS_MODE, 
        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 
     } 

     WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); 
     layoutParams.screenBrightness = 0.5F; 
     getWindow().setAttributes(layoutParams); 
    } 
} 
+0

fa questo aiuto? http://stackoverflow.com/questions/6158628/android-short-screen-brightness-code Scusa se non sto capendo correttamente la tua domanda. – Kgrover

risposta

2

È possibile impostare la luminosità su automatico impostando utilizzando i seguenti codici, i suoi lavori per me.

layoutParams.screenBrightness=-1; 
getWindow().setAttributes(layoutParams); 

Ecco la codifica completa:

public class MainActivity extends Activity { 

WindowManager.LayoutParams layoutParams; 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    try { 
     adjustBright(); 
    } catch (SettingNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Button btn=(Button)findViewById(R.id.button1); 

    btn.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      layoutParams.screenBrightness=-1; 
      getWindow().setAttributes(layoutParams); 
     } 
    }); 



} 

private void adjustBright() throws SettingNotFoundException { 
    // TODO Auto-generated method stub 
    int brightnessMode = Settings.System.getInt(getContentResolver(), 
      Settings.System.SCREEN_BRIGHTNESS_MODE); 
    if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { 
     Settings.System.putInt(getContentResolver(), 
       Settings.System.SCREEN_BRIGHTNESS_MODE, 
       Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 
    } 

    layoutParams = getWindow().getAttributes(); 
    layoutParams.screenBrightness = 0.1F; 
    getWindow().setAttributes(layoutParams); 
} 
} 
Problemi correlati