根据android中的周围光线改变亮度

时间:2012-12-31 12:34:34

标签: android screen-brightness

我想根据光线改变屏幕的亮度..

我找到了获取亮度级别和设置亮度的链接。

Change screen brightness onPause (Android app)

How to toggle auto brightness on and off? (not a repeat)

changing screen brightness programmatically in android

Changing screen brightness programmatically (as with the power widget)

但我想根据周围的光线改变亮度 .. 简而言之,我想做与自动亮度相同的工作..

如何检测周围光线?或者我可以触发自动亮度功能吗?

亮度的值范围介于0到255之间。 我想根据周围的光线水平设置亮度等级,不能是0到255.我怎样才能得到这两者之间的比例。

需要帮助。感谢

1 个答案:

答案 0 :(得分:5)

我自己的解决方案:

我从here实施了摇动侦听器。 我也使用了aboce链接来完成它。

我创建了 ShakeBrightService.java

public class ShakeBrightService extends Service {

private ShakeDetector shakeBrightDetector = new ShakeDetector();

@Override
public void onCreate() {
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    shakeBrightDetector.stop();
    shakeBrightDetector.start(this, shakeBrightEventListener);

    return super.onStartCommand(intent, flags, startId);
}

private ShakeEventListener shakeBrightEventListener = new ShakeEventListener() {

    @Override
    public void onShake() {
        setAutoBrightness(true);
    }
};

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void setAutoBrightness(boolean value) {

    Toast.makeText(getApplicationContext(), "AutoBrightness : " + value,
            Toast.LENGTH_SHORT).show();

    if (value) {

        Settings.System.putInt(getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    } else {

        Settings.System.putInt(getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    Intent i = new Intent(getApplicationContext(), MainActivity1.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);   
}

}

假冒更新视图
MainActivity1.java

public class MainActivity1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main1);

    refreshBrightness(getBrightnessLevel());

    Thread t = new Thread() {
        public void run() {
            try {
                sleep(10);
            } catch (InterruptedException e) {
            }
            finish();
        }
    };
    t.start();
}

private void refreshBrightness(float brightness) {

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    if (brightness < 0) {
        lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    } else {
        lp.screenBrightness = brightness;
    }
    getWindow().setAttributes(lp);
}

int getBrightnessLevel() {
    try {
        int value = Settings.System.getInt(getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS);
        // convert brightness level to range 0..1
        value = value / 255;
        return value;
    } catch (SettingNotFoundException e) {
        return 0;
    }
}

FakeActivity正在使用主题android:theme="@android:style/Theme.Translucent.NoTitleBar",因此默认亮度会发生变化。