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



   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里面都有提供的。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值