【问题标题】:Trying to set screen brightness on android phone尝试在安卓手机上设置屏幕亮度
【发布时间】:2011-11-25 01:34:46
【问题描述】:

我正在尝试设置屏幕亮度,但是当我尝试使用 this.getWindow() 获取当前窗口时,我得到了 null。为什么是这样?我将在我的 setBrightness() 方法中发布我的所有代码。

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS,
            brightness);
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness / (float) 255;
window.setAttributes(lp);

【问题讨论】:

  • 您是通过OnClickListener() 或类似的方式拨打getWindow() 吗?
  • 不只是一个活动。该活动是不可见的,并且该方法是从另一个从主活动类接收通知的类中调用的。我实际上希望它只是普通的类,但它似乎必须是一个活动,以便它可以访问 getWindow() 方法?
  • 这里有一个简单的方法希望对你有帮助stackoverflow.com/questions/2937365/…

标签: android null window screen brightness


【解决方案1】:

不要使用 System.putInt()... 你已经在 lp 中设置了 screenBrightness!

以下代码有效:

Window window = getWindow();
float brightness = 100.0f;
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness/255.0f;
window.setAttributes(lp);

【讨论】:

  • 感谢您的意见!尽管如此,仍然从 getWindow() 获得 null 。也许我以某种方式检索了错误的窗口?
  • 验证你的导入我认为你应该导入 android.view.Window
  • 这些是我的视图导入:import android.view.Window;导入android.view.WindowManager;
  • 当你调用 getwindow() 方法时;它在你的 onCreate() 方法上吗?
  • 您似乎从另一个不是 Activity 的类调用 getWindow() 方法,因此您应该将上下文传递给第二个类,您的类的构造函数将是:public SecondClass(Context _context){ this.context = _context; } ,然后您应该使用如下变量上下文调用 getWindow() 方法: this.context.getWindow();希望有帮助
【解决方案2】:

如果您使用的是搜索栏,请尝试这些行,

System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);

【讨论】:

    【解决方案3】:

    如果你从片段调用它,像这样在 getWindow() 之前添加 getActivity()

    getActivity().getWindow().setAttributes(layoutParams);
    

    如果使用 seekbar 不要让你的 brogress 为 0,因为它会使你的屏幕完全变暗(在 android 2.3 上)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多