我的应用程序是一个简单的“Brightness Changer”,它也改变了系统亮度(什么不容易实现:D)。
我实际上设置了所有东西并且它工作正常......除了一个小烦人的事情......每次亮度改变屏幕首先“闪烁”到最高亮度(如100%光)并且在半秒左右后,它更改为正确的亮度。
示例:系统亮度应为 120: 1. 更改为 255(最大值),半秒更改为 120。
这是我的两个类的代码。Dummy 类用于“重置”(应用亮度更改所必需的。)
编辑:顺便说一句。我认为问题是“lp.screenBrightness = 100/100.0f;” 在虚拟活动中。但是没有它它就行不通:/
测试.java
public class Test extends Activity
{
SeekBar seekbar;
TextView value;
TextView debug;
Button button1;
Button button2;
Button button3;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
value = (TextView) findViewById(R.id.textview2);
debug = (TextView) findViewById(R.id.textview3);
seekbar = (SeekBar) findViewById(R.id.seekbar);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
seekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
// TODO Auto-generated method stub
int brightness = progress+1;
double b = brightness/2.55;
double c = Math.round(b*100)/100;
int percent = (int) c;
if (percent==0) {
value.setText("Lightlevel: !MIN!");
}else if (percent==100){
value.setText("Lightlevel: !MAX!");
}else{
value.setText("Lightlevel: "+percent+"%");
}
debug.setText("int brightness: "+brightness+" / double b:"+b+" / double c: "+c+" / int percent: "+percent);
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightness);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
Intent in = new Intent(Test.this,DummyBrightnessActivity.class);
startActivity(in);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 1);
Intent in = new Intent(Test.this,DummyBrightnessActivity.class);
startActivity(in);
}
});
button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 127);
Intent in = new Intent(Test.this,DummyBrightnessActivity.class);
startActivity(in);
}
});
button3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 255);
Intent in = new Intent(Test.this,DummyBrightnessActivity.class);
startActivity(in);
}
});
}
}
DummyBrightnessActivity.java
public class DummyBrightnessActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.dummy);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100/100.0f;
getWindow().setAttributes(lp);
Timer timer2 = new Timer();
timer2.schedule(new TimerTask() {
public void run() {
finish();
}
}, 500);
}
}