I want to change the Brightness, I can use this method:

public static void SetBright(int brightness, Context context) {
    if (isAutoBrightness(context)) {
        stopAutoBrightness(context);
    }
    WindowManager.LayoutParams lp = ((Activity) context).getWindow()
            .getAttributes();
    lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
    ((Activity) context).getWindow().setAttributes(lp);
}

I need an Activity to pass into SetBright(int brightness, Context context);

But now I have to invoke the method SetBright(int brightness, Context context) in a Brocastreceiver. I can use the context in the method onReceive(Context context, Intent intent) but if I quit the app, it doesn't work.

Is there another method that I can use to change the brightness instead of useing an activity?

有帮助吗?

解决方案

start dummy activity and set window parameter using brightness(0 to 1 range-1 for 255). run a timer of 50, 100 or 500 ms whatever. after that finish the activity.

TimerTask finishTask = new TimerTask() {

        @Override
        public void run() {
            BrightActivity.this.finish();
            timer.cancel();
            if (timer != null) {
                timer = null;
            }
        }
    };
    timer.schedule(finishTask,Constants.BRIGHTNESS_REFRESH_DELAY);

其他提示

Primarily I used 500 ms for screen refresh time, but in galaxy s3 it does not work. but 1000 is ok. So screen refresh time is device dependent may be.

if you want to save then try this after setting brightness :

public static void saveBrightness(Context context, int brightness) {
   ContentResolver resolver= context.getContentResolver();
    Uri uri = android.provider.Settings.System
            .getUriFor("screen_brightness");
    android.provider.Settings.System.putInt(resolver, "screen_brightness",
            brightness);
    resolver.notifyChange(uri, null);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top