您的位置:首页 > 其它

Dialog 调用getWindows()函数进行系统设置 背光问题

2015-09-05 09:08 501 查看

   SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);

   

        //进度条绑定最大亮度,255是最大亮度

        seekBar.setMax(255);

        //取得当前亮度

        int normal = Settings.System.getInt(getContentResolver(),

    Settings.System.SCREEN_BRIGHTNESS, 255);

        //进度条绑定当前亮度

        seekBar.setProgress(normal);

       

        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

   

如果是直接使用的话,会出现报错。代码如下:   

@Override

   public void onStopTrackingTouch(SeekBar seekBar) {

    //取得当前进度

    int tmpInt = seekBar.getProgress();

    

    //当进度小于80时,设置成80,防止太黑看不见的后果。

    if (tmpInt < 80) {

     tmpInt = 80;

    }

    

    //根据当前进度改变亮度

    Settings.System.putInt(getContentResolver(),

      Settings.System.SCREEN_BRIGHTNESS, tmpInt);

    tmpInt = Settings.System.getInt(getContentResolver(),

      Settings.System.SCREEN_BRIGHTNESS, -1);

    WindowManager.LayoutParams wl = getWindow()

      .getAttributes();

    float tmpFloat = (float) tmpInt / 255;

    if (tmpFloat > 0 && tmpFloat <= 1) {

     wl.screenBrightness = tmpFloat;

    }

    getWindow().setAttributes(wl);

    

   }

如果是直接调用的话,找不到相关的系统包含文件,要通过获得Ialog函数的句柄,才能够进一步获得相应的系统函数的句柄。代码如下:

  final AlertDialog.Builder dialog_light = new AlertDialog.Builder(

            mcontext);

         dialog_light.setTitle("Sets the system light").setView(ligtSetingView);

            dialog_light.setPositiveButton("Ok",

            new DialogInterface.OnClickListener() {

             public void onClick(DialogInterface dialog_light, int arg1) {

              

              

             int tmpInt = seekBar.getProgress();

            

            //当进度小于80时,设置成80,防止太黑看不见的后果。

            if (tmpInt < 80) {

             tmpInt = 80;

            }

            

            //根据当前进度改变亮度

            Settings.System.putInt(mcontext.getContentResolver(),

              Settings.System.SCREEN_BRIGHTNESS, tmpInt);

            tmpInt = Settings.System.getInt(mcontext.getContentResolver(),

              Settings.System.SCREEN_BRIGHTNESS, -1);

            WindowManager.LayoutParams wl = ((Dialog) dialog_light).getWindow()

              .getAttributes();

            

            float tmpFloat = (float) tmpInt / 255;

            if (tmpFloat > 0 && tmpFloat <= 1) {

             wl.screenBrightness = tmpFloat;

            }

           ((Dialog) dialog_light).getWindow().setAttributes(wl);

              

              ((ViewGroup) ligtSetingView.getParent())

            .removeView(ligtSetingView);

             dialog_light.dismiss();

             }

            }).setNegativeButton("Cancel",

            new DialogInterface.OnClickListener() {

             @Override

             public void onClick(DialogInterface dialog_light, int arg1) {

              

            // TODO Auto-generated method stub

            ((ViewGroup) systemTimeView.getParent())

            .removeView(systemTimeView);

            dialog_light.dismiss();

           

             }

            });

这样子就不会找不到系统函数的相关头文件了,在这个头文件是在相应的API里面都有提供的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: