Update Display Brightness on Android after changing it programmatically

前端 未结 3 1465
旧巷少年郎
旧巷少年郎 2020-12-15 11:02

I\'m trying to update the display brightness from a widget but i have some problems.

To change brightness level, i use:

Settings.System.putInt(contex         


        
3条回答
  •  一生所求
    2020-12-15 11:24

    Can you use this code in your RemoteView,

    Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
    
    // This makes the new screen brightness effective
    WindowManager.LayoutParams layoutParams = ((Activity)context).getWindow().getAttributes();
    float b = brightness/255.0f;
    if(b == 0.0)    
        b = 0.01f;
    layoutParams.screenBrightness = b;
    ((Activity)context).getWindow().setAttributes(layoutParams);
    

    This code fine works when you are setting phone screen brightness from inside a User-defined class which is not extending an Activity but you only need the context.

提交回复
热议问题