我试图模仿可以控制设备亮度的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"));
}
答案 0 :(得分:0)
你没有刷新屏幕。所以亮度不会改变。
Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 20);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.2f;// 100 / 100.0f;
getWindow().setAttributes(lp);
startActivity(new Intent(this,RefreshScreen.class));
创建一个名为RefreashScreen的虚拟活动,它只调用finish。 20是亮度等级的int,所以用你想要的替换它。我希望这会有所帮助。
答案 1 :(得分:0)
NightSky的答案几乎是正确的,但我想我知道为什么它不适合你。实际上,您必须制作一个透明的“虚拟”活动来处理亮度变化并刷新它。我不确定它如何与SeekBar一起工作,但这是我能想到的唯一可以刷新Window的方法。
将以下代码放在透明活动的onCreate()中(不在当前活动中,但在虚拟活动中): -
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness; //Use your brightness value or an arbitrary one
getWindow().setAttributes(lp);
/*Here is the real difference. You need to allow some time for you screen to be
refreshed before you call finish() on your activity. This means we should make a
thread that calls finish() after a small amount of time, say 300ms (You can use a
different value if you want). */
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 300);
这对我来说过去很有用。正如我之前提到的,我不认为有一种方法可以让你使用SeekBar,当你改变它的值时,它会不断改变亮度。你必须刷新屏幕。
答案 2 :(得分:0)
以下是我的工作代码(作为小部件):
基本上,它会启动一个进行调整的Activity。
BrightnessActivity.java:
package com.xxx.switchwidget;
import com.xxx.Utils;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
public class BrightnessActivity extends Activity {
private static final boolean DEBUG = true;
private static final String TAG = "BrightnessActivity";
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
finish();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int brightness = SwitchWidget.getNextBrightness(this);
if (brightness < 0) {
finish();
return;
}
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = Float.valueOf(brightness / Float.valueOf(SwitchWidget.MAXIMUM_BACKLIGHT)).floatValue();
getWindow().setAttributes(params);
toggleBrightness();
}
@Override
public void onDestroy() {
super.onDestroy();
if (DEBUG)
Utils.log(TAG, "onDestroy()");
}
private void toggleBrightness() {
new AsyncTask<Void, Integer, Void>() {
@Override
protected Void doInBackground(Void... args) {
SwitchWidget.toggleBrightness(BrightnessActivity.this);//TODO: Change to your own Activity
publishProgress(1);
return null;
}
@Override
protected void onProgressUpdate(Integer... args) {
if (DEBUG)
Utils.log(TAG, "onProgressUpdate");
mHandler.sendEmptyMessage(0);
}
@Override
protected void onPostExecute(Void result) {
}
}.execute();
}
}
您的Activity(Tab)中的方法:(我的代码中是* SwitchWidget ) *
public static void toggleBrightness(Context context) {
try {
ContentResolver cr = context.getContentResolver();
int brightness = Settings.System.getInt(cr,
Settings.System.SCREEN_BRIGHTNESS);
boolean isBrightnessModeChanged = false;
int brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
brightnessMode = Settings.System.getInt(cr,
Settings.System.SCREEN_BRIGHTNESS_MODE);
// Rotate AUTO -> MINIMUM -> DEFAULT -> MAXIMUM
// Technically, not a toggle...
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
brightness = DEFAULT_BACKLIGHT;
brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
isBrightnessModeChanged = true;
} else if (brightness < DEFAULT_BACKLIGHT) {
brightness = DEFAULT_BACKLIGHT;
} else if (brightness < MAXIMUM_BACKLIGHT) {
brightness = MAXIMUM_BACKLIGHT;
} else {
brightness = MINIMUM_BACKLIGHT;
}
// Set screen brightness mode (automatic or manual)
if (isBrightnessModeChanged) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, brightnessMode);
}
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) {
Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS,
brightness);
}
} catch (Settings.SettingNotFoundException e) {
if (DEBUG)
Utils.log(TAG, "toggleBrightness: " + e);
}
}
最后调用它来设置亮度:
Intent newIntent = new Intent(mContext, BrightnessActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(newIntent);
希望这可能会有所帮助。