Android编程获取并设置Activity亮度的方法
短信预约 -IT技能 免费直播动态提醒
本文实例讲述了Android编程获取并设置Activity亮度的方法。分享给大家供大家参考,具体如下:
此API只适合2.1以上版本:
public static boolean isAutoBrightness(ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt(aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return automicBrightness;
}
public static int getScreenBrightness(Activity activity) {
int nowBrightnessValue = 0;
ContentResolver resolver = activity.getContentResolver();
try {
nowBrightnessValue = android.provider.Settings.System.getInt(
resolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
e.printStackTrace();
}
return nowBrightnessValue;
}
public static void setBrightness(Activity activity, int brightness) {
// Settings.System.putInt(activity.getContentResolver(),
// Settings.System.SCREEN_BRIGHTNESS_MODE,
// Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
activity.getWindow().setAttributes(lp);
}
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
public static void saveBrightness(ContentResolver resolver, int brightness) {
Uri uri = android.provider.Settings.System
.getUriFor("screen_brightness");
android.provider.Settings.System.putInt(resolver, "screen_brightness",
brightness);
resolver.notifyChange(uri, null);
}
使用SeekBar进行亮度控制:
sSeekBar = (SeekBar) findViewById(R.id.seek_bar);
sSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (progress < 10) {
} else {
setBrightness(Test2Activity.this, progress);
}
}
});
//获取当前亮度的位置
int a =getScreenBrightness(this);
sSeekBar.setProgress(a);
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:max="255"
android:maxHeight="8dp"
android:minHeight="8dp"
android:paddingBottom="5dp"
android:paddingLeft="13dp"
android:paddingRight="13dp"
android:progress="0"
/>
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android入门之Activity四种启动模式(standard、singleTop、singleTask、singleInstance)Android Activity启动模式之singleTop实例详解Android Activity启动模式之standard实例详解Android Activity中onStart()和onResume()的区别分析Android的Activity加载方式实例分析Android编程之四种Activity加载模式分析Android编程开发之TextView单击链接弹出Activity的方法Android编程四大组件之Activity用法实例分析深入解读Android开发中Activity的生命周期Android Activity启动模式之singleTask实例详解
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341