How can I change the Brightness of the screen in BroadcastReceiver?
android, screen-brightness
Solution
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);
Problem
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?