获取当前屏幕亮度

时间:2015-10-11 09:57:41

标签: android

我想从背景中获得真正的亮度值。我尝试了几种方法:

1

    curBrightnessValue =android.provider.Settings.System.getInt(
                getContext().getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS);

但如果屏幕亮度处于自动模式,则该值保持不变。

  1. 阅读sys/class/backlight/brightness/

    这是一个很好的方法,但我想要一种不读文件的方式。

3 个答案:

答案 0 :(得分:2)

据我所知,在自动模式下无法以任何其他方式完成。请参阅this answer

答案 1 :(得分:2)

使用以下代码获取背景亮度(如果您愿意,还可以更改亮度值):

Settings.System.putInt(
    cResolver,
    Settings.System.SCREEN_BRIGHTNESS_MODE,
    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
brightness =
    Settings.System.getInt(
        cResolver,
        Settings.System.SCREEN_BRIGHTNESS);  
System.out.println("Current Brightness level " + brightness);

答案 2 :(得分:0)

在自动模式下使用Settings.System.getInt()所述的方法1不适用于较旧的Android版本(如“ N”)。但是它适用于'P',并且与文件/sys/class/backlight/panel0-backlight/brightness中的值相同。 我在Processing

中尝试过的一些示例代码
import android.provider.Settings;  // for global system settings
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

Activity act;
Context context;

void setup() {
    act = this.getActivity();
    context = act.getApplicationContext();
}

void draw() {
    text("brightness = " + getBrightness());
}

float getBrightness() {
    float brightness;
    if(!Settings.System.canWrite(context)) {
        // Enable write permission
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        context.startActivity(intent);
    } else {
        // Get system brightness
        Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); // enable auto brightness
        brightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, -1);  // in the range [0, 255]
    }
    return brightness;
}