Changing backlight brightness in Appwidget is not responding instantly

987 views Asked by At

I have created a simple Appwidget to control the brightness of the screen by this code:

android.provider.Settings.System.putInt(context.getContentResolver(), 
                  android.provider.Settings.System.SCREEN_BRIGHTNESS, 50);

When i click the widget, there is no change in the brightness of the backlight immediately. But when i lock and unlock the phone, it works. I read Changing screen brightness programmatically (as with the power widget) that using dummy activity will give the solution. So i created an activity like this

public class Bright extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.finish();

}
}

In my AppWidget class i used intent like this:

context.startActivity(new Intent(context, Bright.class));

But when i click the widget, it force closes. Is my implementation is correct for my AppWidget??

1

There are 1 answers

1
Glitch On

You should adjust the brightness in your dummy Activity too by using getWindow().getAttributes().screenBrightness if you want it to respond immediately.

As for the crashing, you might want to post the stack trace. But first check that you added it to your manifest.