1

I want a screen flashing feature i.e blinking in my project. I followed this question link in stackoverflow Adding screen brightness controls to android application

I applied the code given by fiXedd there. It works i mean when a state is true i am flashing and code is working but then say state is false(i.e i want to stop flashing and restore old brightness) then my whole screen goes black. Then i have to press back to exit app.

What is the better way to do screen flashing and then later based on condition to stop it ? Thanks.

Community
  • 1
  • 1
padam thapa
  • 1,471
  • 2
  • 20
  • 41

1 Answers1

1

First you need to store the brightness of the screen before flashing in some variable example float

float oldBrightNess = Settings.System.getInt(getContentResolver(), 
                 Settings.System.SCREEN_BRIGHTNESS);

and set this value to window layout when the state is false.

See the following code it works exactly same way. If the screen is not flashed then clickin on the button will flash and vice-versa when you click once again

Sample Code

public class RelativeLayoutTesting extends Activity implements OnClickListener {
    Button button3;
    boolean isPLAYING;
    MediaPlayer mp;
    boolean isFlashedBefore = false;
    float oldBrightNess = 0;
    float newBrightNess = 1.0f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample);
        button3 = (Button) findViewById(R.id.btn3);
        button3.setOnClickListener(this);
        try{
        oldBrightNess = Settings.System.getInt(getContentResolver(), 
                 Settings.System.SCREEN_BRIGHTNESS);
        }catch(Exception ex){
            ex.printStackTrace();
            oldBrightNess = 1.0f;
        }
System.out.println("...Brighness..."+oldBrightNess);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        if(!isFlashedBefore){

        lp.screenBrightness = newBrightNess;

        }else{
            lp.screenBrightness = oldBrightNess;
        }
        getWindow().setAttributes(lp);
        isFlashedBefore = !isFlashedBefore;

//      emailWOAttach(RelativeLayoutTesting.this);
    }
}
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • not working. I am doing screen flashing animation repeatedly on a handler thread. When i restore it doesn't work. screen goes black. – padam thapa Dec 01 '11 at 08:24