【问题标题】:android - Setting the screen brightness to maximum levelandroid - 将屏幕亮度设置为最高级别
【发布时间】:2017-04-26 17:14:44
【问题描述】:

我是这个 android 开发领域的新手。这些天我正在开发一个应用程序,我想在打开应用程序后将屏幕亮度设置为最大级别,并在我退出应用程序后将其设置回之前的级别。有人可以为此提供完整的源代码吗?关于这个问题,我几乎阅读了 stackoverflow 中的每个线程。而且我不明白将这些建议的代码放在哪里。但是如果你能想出完整的代码,那么我就能理解一切。谢谢!

【问题讨论】:

    标签: android screen-brightness


    【解决方案1】:

    我的一个朋友给我发了一个更好、更简单的代码来解决他从互联网上找到的这个问题

    对于业余爱好者(如我),您必须在“setContentView(R.layout.activity_main);”之后输入以下代码在 MainActivity.java 中的“protected void onCreate(Bundle savedInstanceState) {”方法中

        WindowManager.LayoutParams layout = getWindow().getAttributes();
        layout.screenBrightness = 1F;
        getWindow().setAttributes(layout);
    

    顺便说一句,感谢@AbdulKawee 抽出宝贵的时间和您为我提供代码的支持。真的很感激:)

    【讨论】:

    • 完美运行。如果您只需要它来进行这一项活动,这看起来像是要走的路。如果您也想更改其他应用的亮度,则需要接受答案中提供的方法。
    【解决方案2】:

    你可以用这个

    public class MainActivity extends AppCompatActivity {
    
    private int brightness=255;
    //Content resolver used as a handle to the system's settings
    private ContentResolver cResolver;
    //Window object, that will store a reference to the current window
    private Window window;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        cResolver = getContentResolver();
    
        //Get the current window
        window = getWindow();
    
        try
        {
            // To handle the auto
    
    
            Settings.System.putInt(cResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            //Get the current system brightness
            brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
        }
        catch (Settings.SettingNotFoundException e)
        {
            //Throw an error case it couldn't be retrieved
            Log.e("Error", "Cannot access system brightness");
            e.printStackTrace();
        }
    
        Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
        //Get the current window attributes
        WindowManager.LayoutParams layoutpars = window.getAttributes();
        //Set the brightness of this window
        layoutpars.screenBrightness = brightness / (float)100;
        //Apply attribute changes to this window
        window.setAttributes(layoutpars);
     }
    }
    

    以及清单中最重要的权限

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

    在 API 23 中请求写入设置权限

    private boolean checkSystemWritePermission() {
    boolean retVal = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        retVal = Settings.System.canWrite(this);
        Log.d(TAG, "Can Write Settings: " + retVal);
        if(retVal){
            Toast.makeText(this, "Write allowed :-)", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(this, "Write not allowed :-(", Toast.LENGTH_LONG).show();
           Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
           intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
           startActivity(intent);
        }
      }
     return retVal;
    }
    

    【讨论】:

    • 如果您不介意,能否将整个源代码(从上到下)粘贴到此处,就像在 Mainactivity.java 中一样。这样我就可以弄清楚我的代码有哪些更改以及需要在哪里进行更改。 (我确实理解清单的事情。所以你不需要在这里再次粘贴该代码。)谢谢!
    • 我得实现它,等等
    • 嘿,我正在编辑我的代码并在此处粘贴完整的代码
    • 它对我有用!非常感谢。但还有另一个问题。它适用于我的 API 15 手机。但它不适用于我的 API 23 手机。这可能是什么原因?
    • @SankhaRathnayake 问题是启动 API 23 您必须明确允许权限,为此转到设置->应用程序或应用程序管理->修改系统设置,然后检查您的应用程序并允许权限
    【解决方案3】:

    Kotlin 版本的 Sankha Rathnayake 的回答:

    val attributes = window.attributes
    attributes.screenBrightness = 1f
    window.attributes = attributes
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多