0

在我的 android 应用程序中,我想要一个按钮,其中第一次点击亮度为 10,第二次点击亮度为 50,第三次点击亮度为 255(完整),因为用户点击下一步,它将再次回到 10 如何代码这个我已经厌倦了这个解决方案,但只能增加

 private void getBrightness() {
    try 
        {   int curBrightnessValue = 0 ;
            curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
            if(curBrightnessValue >= 250  ) 
            {

            int SysBackLightValue = curBrightnessValue-30;


                   android.provider.Settings.System.putInt(getContentResolver(),
                   android.provider.Settings.System.SCREEN_BRIGHTNESS,
                   SysBackLightValue);
                   curBrightnessValue=SysBackLightValue;
            }
            else if(curBrightnessValue <250)

            {

                 int SysBackLightValue = curBrightnessValue+30;


                android.provider.Settings.System.putInt(getContentResolver(),
                   android.provider.Settings.System.SCREEN_BRIGHTNESS,
                   SysBackLightValue);
                   curBrightnessValue=SysBackLightValue;



            }

请帮我解决这个问题

4

1 回答 1

1

您的代码甚至没有按照您在问题中所说的去做(您只尝试增加和减少 30 甚至没有应用它),您正在以正确的方式获得亮度,您所要做的就是处理Button Click Listener 中的新值:

    private int getBrightness() {
        int curBrightnessValue = 0 ;
         try 
            {   
              curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
            }
        catch(Exception ex){
              curBrightnessValue = 0
            }
        return curBrightnessValue;
    }

    ok.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            int current_brightness = getBrightness();
            int new_brightness;
            if(current_brightness > 250)
                 new_brightness = 10;
            else if(current_brightness >= 50)
                 new_brightness = 255;
            else if(current_brightness >= 10)
                 new_brightness = 50;
            else
                 new_brightness = 150; // let's say this will be the default value
           // and to set the brightness
           set_system_brightness(new_brightness);
         }
    });

    private void set_system_brightness(int new_brightness){
        Settings.System
        .putInt(getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS,
                new_brightness); // Set the system
                            // brightness
        android.view.WindowManager.LayoutParams w_lp = getWindow()
                .getAttributes(); // Get the current window
                                    // attributes
        w_lp.screenBrightness = new_brightness / (float) 255; // Set
                                                        // the
                                                        // brightness
                                                        // of
                                                        // this
                                                        // window
        getWindow().setAttributes(w_lp); // Apply attribute
                                            // changes to
                                            // this window
    }
于 2016-04-19T12:30:06.587 回答