Android modify system brightness and set system volume

1. Application permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

If you are smart, you will find that there are two user permissions here. One is the sdk write permission, and the other is the modification system setting permission, which is a special permission.
In android 6.0 and later, the protection level of the WRITE_SETTINGS permission has been upgraded from the original dangerous to signature, which means that our APP needs to be signed by the system or become the pre-installed software of the system to apply for this permission, and also need to prompt the user to jump to Modify the system's settings interface to grant this permission.
Many great gods have already given the answer:
 

 //申请android.permission.WRITE_SETTINGS权限的方式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //如果当前平台版本大于23平台
            if (!Settings.System.canWrite(this)) {
                //如果没有修改系统的权限这请求修改系统的权限
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivityForResult(intent, 0);
            } else {
                //有了权限,你要做什么呢?具体的动作

            }
        }

When you install the demo, you will enter the following interface: Only with our permission can you modify the screen brightness of the system.

Write picture description here

2. Commonly used methods for system brightness
1. The mode to obtain the current screen brightness

/** 
 2. SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度 
 3. SCREEN_BRIGHTNESS_MODE_MANUAL=0  为手动调节屏幕亮度 
 */  
      private int getScreenMode(){  
        int screenMode=0;  
        try{  
    screenMode =Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);  
        }  
        catch (Exception localException){  

        }  
        return screenMode;  
      }  

2. Get the current screen brightness value 0-255

 /** 
  * 获得当前屏幕亮度值  0--255 
  */  
      private int getScreenBrightness(){  
        int screenBrightness=255;  
        try{  
 screenBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  
        }  
        catch (Exception localException){  

        }  
        return screenBrightness;  
      }  

3. Set the mode of the current screen brightness

 /** 
     * 设置当前屏幕亮度的模式     
     * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度 
     * SCREEN_BRIGHTNESS_MODE_MANUAL=0  为手动调节屏幕亮度 
     */  
      private void setScreenMode(int paramInt){  
        try{  
          Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, paramInt);  
        }catch (Exception localException){  
          localException.printStackTrace();  
        }  
      }  

4. Set the current screen brightness value 0–255

/** 
       * 设置当前屏幕亮度值  0--255 
       */  
      private void saveScreenBrightness(int paramInt){  
        try{  
          Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, paramInt);  
        }  
        catch (Exception localException){  
          localException.printStackTrace();  
        }  
      }  

5. Save the current screen brightness value and make it effective

 /** 
      * 保存当前的屏幕亮度值,并使之生效 
      */  
      private void setScreenBrightness(int paramInt){  
        Window localWindow = getWindow();  
        WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();  
        float f = paramInt / 255.0F;  
        localLayoutParams.screenBrightness = f;  
        localWindow.setAttributes(localLayoutParams);  
      }  

 3. System volume adjustment

    var mAudioManager: AudioManager? = null



    // 最大音量
    var maxVolume = 0
    // 当前音量
    var currentVolume:Int = 0

    /**
     * 初始化音量数据
     *
     * @description:
     * @author ldm
     * @date 2016-12-2 下午3:20:05
     */
    fun initVolume() {
        mAudioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        // 获取系统最大音量
        maxVolume = mAudioManager?.getStreamMaxVolume(AudioManager.STREAM_MUSIC)!!
        // 设置voice_seekbar的最大值
        sbSpeakerVolume.setMax(maxVolume);
        // 获取到当前 设备的音量
        currentVolume = mAudioManager?.getStreamVolume(AudioManager.STREAM_MUSIC)!!
        NLog.e("-------------当前音量:"+currentVolume+"-----maxVolume:"+maxVolume)
    }


   

Set self media volume
 

// 设置音量
mAudioManager?.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);

Guess you like

Origin blog.csdn.net/u013512708/article/details/119827805