简体   繁体   English

android - 将屏幕亮度设置为最高级别

[英]android - Setting the screen brightness to maximum level

I'm a newbie to this field of android development. 我是这个Android开发领域的新手。 These days i'm developing an app and I want to set the screen brightness to maximum level once I open the app, and set it back to the previous level once I exit the app. 这些天我正在开发一个应用程序,我想在打开应用程序后将屏幕亮度设置为最高级别,并在退出应用程序后将其设置回上一级别。 Can someone come up with the full source code for this? 有人能想出完整的源代码吗? I read almost every thread in stackoverflow regarding this issue. 我读了几乎关于这个问题的stackoverflow中的每个线程。 And I couldn't understand where to put those suggested codes. 我无法理解在哪里提出这些建议的代码。 But if u can come up with the full code, then I'll be able to understand everything. 但是,如果你能提出完整的代码,那么我将能够理解一切。 Thanks! 谢谢!

One of my friends sent me a better, simple code to fix this issue which he has found from the internet 我的一位朋友给我发了一个更好,更简单的代码来解决他从互联网上找到的这个问题

For amateurs (like me), you have to enter below code after "setContentView(R.layout.activity_main);" 对于业余爱好者(像我一样),你必须在“setContentView(R.layout.activity_main);”之后输入以下代码。 in "protected void onCreate(Bundle savedInstanceState) {" method in your MainActivity.java 在MainActivity.java中的“protected void onCreate(Bundle savedInstanceState){”方法中

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

Btw, Thank you @AbdulKawee for your time and the support that u gave me with your code. 顺便说一下,谢谢你@AddulKawee的时间和你给我的代码支持。 really appreciate it :) 非常感谢:)

You can use this 你可以用它

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);
 }
}

And the most important permission in the manifest 清单中最重要的许可

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

Asking for Write Settings permission in API 23 在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM