2

我的 Eclipse 中有 2 个应用程序,它们都调用了该方法

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

但是一个工作正常,另一个根本不工作(一段时间后屏幕仍然会完全变暗,并出现锁定屏幕)。我使用的代码完全相同,我很沮丧为什么它不能在第二个应用程序上运行。有人知道这是怎么发生的吗?

这是我的第二个应用程序的代码:

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")&&!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));
        }
    };

编辑:我在我想在我的第二个应用程序上变暗的活动上运行镀铬仪表和进度条。

4

0 回答 0