FLAG_KEEP_SCREEN_ON is not working properly
<p>I have 2 apps in my eclipse, they both called the method </p>

<pre><code>        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
</code></pre>

<p>But one works fine and other didn't work at all(the screen will still be dimmed completely after some time, and lock screen appeared). The codes I am using is exactly the same and I am frustrated why it didn't work on the second apps. Anyone has any clue how this is happening?</p>

<p>Here is the code of my second app:</p>

<pre><code>private void DimScreen()   //isDim is false initially
{
    Log.i(TAG, "isDim"+String.valueOf(isDim));
    SharedPreferences pref = getSharedPreferences(null, 0);
    String enableDim = pref.getString("enableDimScreen", "true"); //default preference is true(to dim screen)
    Log.i(TAG, "inside Dimscreen(), enableDim="+enableDim);
    if(enableDim.equals("true")&amp;&amp;!isDim)    //if the user want to dim the screen
    {
        Toast.makeText(StartActivity.this, "Dimming screen in 5 seconds, press Stop button to turn on the screen", Toast.LENGTH_SHORT).show();
        handler.postDelayed(r, 5000);
    }
    else {
        if(!isDim)  //if the user doesn't want to dim the screen
        {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            isDim =true;
            Log.i(TAG, "inside Dimscreen() else");
        }
    }

}
private Handler handler= new Handler();
    Runnable r = new Runnable()
    {
        public void run()
        {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness=0.01f;
            getWindow().setAttributes(lp);  
            isDim =true;
            Log.i(TAG, "isDim "+String.valueOf(isDim));
        }
    };
</code></pre>

<p>EDIT:
I have chrome meter and progress bar running on the activity that I want to dim on my second app.</p>
