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

San*_*ake 3 android screen-brightness

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

San*_*ake 6

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

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

    WindowManager.LayoutParams layout = getWindow().getAttributes();
    layout.screenBrightness = 1F;
    getWindow().setAttributes(layout);
Run Code Online (Sandbox Code Playgroud)

顺便说一句,谢谢@AbdulKawee给您的时间,以及您为我提供的代码支持。真的很感激:)

  • 效果很好。如果您只需要它来完成一项活动,这看起来是一个不错的选择。如果您也想更改其他应用程序的亮度,则需要接受的答案中提供的方法。 (2认同)

Abd*_*wee 5

你可以用这个

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);
 }
}
Run Code Online (Sandbox Code Playgroud)

以及清单中最重要的权限

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Run Code Online (Sandbox Code Playgroud)

在 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;
}
Run Code Online (Sandbox Code Playgroud)