我正在尝试模仿可以控制设备亮度的Andorid设置小部件。它按预期工作,我可以在设置中看到。但我没有看到设备变亮。
这是代码:
WindowManager.LayoutParams lp = getWindow().getAttributes();
SeekBar brightnessControl = (SeekBar) findViewById(R.id.sbBrightness);
int currentBrightness = 10;
brightnessControl.setProgress(currentBrightness);
lp.screenBrightness = currentBrightness/100f;
brightnessControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
//set local brightness
WindowManager.LayoutParams lp = getWindow().getAttributes();
progress = (progress <= 10)?10:progress;
lp.screenBrightness = progress/100f;
getWindow().setAttributes(lp);
//put local to system wide brightness
int sysWideBrightness = (int) (progress/100f * 255);
android.provider.Settings.System.putInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
sysWideBrightness);
}
});
我会提到这个活动是在一个活动选项卡上触发的。在将其放入选项卡之前,它可以正常工作,但并非如此。
我注意到当我在我的设备上安装一个新版本的 android(现在是 4.2.2 根)时,它能够按预期工作。我在有根姜饼设备上对其进行了测试,但它不起作用。
这可能会有所帮助,这是 Main.java 文件,这里是 ActivityTab 的启动方式。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* TabHost will have Tabs */
tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");
TabSpec fifthTabSpec = tabHost.newTabSpec("tid5");
firstTabSpec.setIndicator("", getResources().getDrawable(R.drawable.clear_cache_tab)).setContent(showClearCache());
secondTabSpec.setIndicator("", getResources().getDrawable(R.drawable.move_sd_tab)).setContent(showMoveToSd());
thirdTabSpec.setIndicator("", getResources().getDrawable(R.drawable.remove_app_tab)).setContent(showRemoveApp());
fourthTabSpec.setIndicator("", getResources().getDrawable(R.drawable.feature_manager_tab)).setContent(showFeatureManager());
fifthTabSpec.setIndicator("", getResources().getDrawable(R.drawable.feature_manager_tab)).setContent(showProcessKill());
tabHost.setOnTabChangedListener(this);
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
tabHost.addTab(fourthTabSpec);
tabHost.addTab(fifthTabSpec);
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#303030"));
}
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#C8C8C8"));
}
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#303030"));
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#C8C8C8"));
}