0

I wrote this code

    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
    params.screenBrightness = 0.1F;
    getWindow().setAttributes(params);

    SystemClock.sleep(5000);

    TextView Text01 = (TextView) findViewById(R.id.Text1);
    Text01.setText("Hello");

to set the screen brightness and then, after 5 seconds, it displays text. The problem is that it waits 5 secs then Android dims the screen and output the text at the same time. Why it does not work sequentially? Thanks

Ajay Soman
  • 1,631
  • 4
  • 19
  • 37
notebook
  • 29
  • 2
  • 6

1 Answers1

0

My guess is that code is running in the UI thread, which means nothing gets drawn to the UI until your function exits. You might want to set up a callback of some kind to trigger the change of text in the future. Maybe an AsyncTask, where the sleeping happens in the background thread and the UI altering code happens in onPostExecute.

noisecapella
  • 814
  • 7
  • 17
  • THanks, I think that is UI problem too. I have read a script at http://stackoverflow.com/questions/10368919/why-imageview-cant-update-before-systemclock-sleep which has the same issue so I tried, it works. However I cannot extend to let arbitrary wait between commands, say 5-6 commands which need to change screen colour/brightness at different time delay. Do you have any idea to go through that? – notebook Sep 03 '12 at 23:02
  • Use a [Handler](http://developer.android.com/reference/android/os/Handler.html). They have methods which allow for arbitrary delays, like `sendEmptyMessageDelayed`. – noisecapella Sep 03 '12 at 23:06
  • But can I have more than one Handler? as after each command i need one. Also the problem is UI does not allow update until the last command, so any technique to overcome this? – notebook Sep 03 '12 at 23:27