首页 > 其他分享 >android 设置亮度相关方法

android 设置亮度相关方法

时间:2023-03-23 13:58:00浏览次数:48  
标签:设置 Settings SCREEN 亮度 context Context android 屏幕

package com.example.lightsensortest;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

//需要权限  <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
public class BrightnessA {

   
//屏幕亮度最亮255,最暗0
     //获取系统亮度值
    public static int getScreenBrightness(Context context){
//        setBrightnessManualMode(context);
        ContentResolver contentResolver=context.getContentResolver();
        int defaultValue=-1;
        return Settings.System.getInt(contentResolver,
                Settings.System.SCREEN_BRIGHTNESS, defaultValue);
    }


//获取当前app 当前屏幕亮度值
    public static float getCurrentAppBrightness(Context context) {
        Window window = ((AppCompatActivity)context).getWindow();
        WindowManager.LayoutParams lp=window.getAttributes();
        return window.getAttributes().screenBrightness *255;
    }




    
    //设置屏幕亮度
     
    //注:屏幕亮度最亮255,最暗0
     
    public static void setScreenBrightness(int brightness,Context context) {
        if(brightness<0||brightness>255){
            throw new SecurityException("=======亮度值需设置在 0-255 之间的int值=======");
        }

        //设置为手动模式
        setBrightnessManualMode(context);
        //设置屏幕亮度
        Window window = ((AppCompatActivity)context).getWindow();
        WindowManager.LayoutParams lp=window.getAttributes();
        lp.screenBrightness = brightness / 255.0f;
        window.setAttributes(lp);
    }

    /**设置屏幕最亮**/
    public static void setMaxBrightness(Context context){
        //设置为手动模式
        setBrightnessManualMode(context);
        //设置屏幕亮度
        setScreenBrightness(255,context);
    }

    /**设置屏幕最暗**/
    public static void setMinBrightness(Context context){
        //设置为手动模式
        setBrightnessManualMode(context);
        //设置屏幕亮度
        setScreenBrightness(0,context);
    }

   
    //设置屏幕常亮
    //需要在界面调用 "setContentView(R.layout.布局id);" 之前调用。
     
    public static void setScreenKeepOn(Context context){
        ((AppCompatActivity)context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    
    //清除屏幕常亮
     
    //常在activity的onPause()周期内调用(仅供参考)
    
    public static void cancelScreenKeepOn(Context context){
        ((AppCompatActivity)context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }


    
     //设置亮度调节模式为手动模式
     
     //SCREEN_BRIGHTNESS_MODE_AUTOMATIC:自动调节模式
     //SCREEN_BRIGHTNESS_MODE_MANUAL:手动调节模式
    
    private static void setBrightnessManualMode(Context context){
        ContentResolver contentResolver=context.getContentResolver();
        int model= 0;
        try {
            model = Settings.System.getInt(contentResolver,Settings.System.SCREEN_BRIGHTNESS_MODE);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        if(model==Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC){
            Settings.System.putInt(
                    contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        }
    }

}

  

标签:设置,Settings,SCREEN,亮度,context,Context,android,屏幕
From: https://www.cnblogs.com/cwxkurenoki/p/17247207.html

相关文章