这内容挺用的,特别是需要调节亮度的程序,自己收藏起来,呵呵。这段代码不只能改变当前的Activity,也可以改变整个System的亮度
代码:
package com.jimmy;
import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.MyTextView);
updateToggles();
}
private void updateToggles() {
// TODO Auto-generated method stub
SeekBar seekBar = (SeekBar) findViewById(R.id.MySeekBar);
seekBar.setProgress((int) (android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, 255) ));
seekBar.setOnSeekBarChangeListener(seekListener);
}
private OnSeekBarChangeListener seekListener = new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
Integer tmpInt = seekBar.getProgress();
System.out.println(tmpInt);
// 51 (seek scale) * 5 = 255 (max brightness)
// Old way
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
tmpInt); // 0-255
tmpInt = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, -1);
// Cupcake way..... sucks
WindowManager.LayoutParams lp = getWindow().getAttributes();
// lp.screenBrightness = 1.0f;
// Float tmpFloat = (float)tmpInt / 255;
if (0<= tmpInt && tmpInt <= 255) {
lp.screenBrightness = tmpInt;
}
getWindow().setAttributes(lp);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
// put awesomeness here
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
// and here too
}
};
}这是主程序