gpt4 book ai didi

java - 安卓SDK : How do you set the screen brightness

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:35 26 4
gpt4 key购买 nike

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

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

最佳答案

您可以使用以下代码将亮度设置回自动,这对我有用。

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

关于java - 安卓SDK : How do you set the screen brightness,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com