5

我一直在环顾四周,我只能找到可以设置该活动亮度的代码。我正在尝试更改实际的电话设置。我试过的代码是:

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

1 回答 1

2

您可以通过使用以下编码将亮度设置回自动,它对我有用。

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

这是完整的编码:

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);
}
}
于 2012-10-17T15:56:37.977 回答