4

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?

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
smileVann
  • 554
  • 1
  • 6
  • 15
  • hi irain, i just wanna to confirm u want to modify system brightness permanently ? – ρяσѕρєя K May 21 '12 at 15:45
  • http://stackoverflow.com/questions/5032588/cant-apply-screen-brightness-settings-programmatically-in-android – Andrey Ermakov May 21 '12 at 15:57
  • I'm curious as to what you're trying to do with this. Seems roundabout at first glance. There might be a better solution, can you explain more? – Phix May 21 '12 at 16:05
  • Thanks 1st,what i want to do is how can i change the brightness of the phone in broadcastreceiver instead of in an activity.the problem is getWindow() is the method of activity. PS,i know how to save the brightness,it's not the problem. – smileVann May 21 '12 at 23:22

3 Answers3

4

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);
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
makripon
  • 66
  • 2
1

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.

makripon
  • 66
  • 2
0

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);
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks 1st,what i want to do is how can i change the brightness of the phone in broadcastreceiver instead of in an activity.the problem is getWindow() is the method of activity. PS,i know how to save the brightness,it's not the problem. – smileVann May 21 '12 at 23:22