android -将屏幕亮度设置为最大水平

j2datikz  于 2023-04-18  发布在  Android
关注(0)|答案(4)|浏览(134)

我是一个新手到这个领域的android开发.这些天我正在开发一个应用程序,我想设置屏幕亮度到最大水平,一旦我打开应用程序,并设置回以前的水平,一旦我退出应用程序.有人能拿出完整的源代码,这?我读了几乎每一个线程在stackoverflow关于这个问题.我不能理解在哪里把这些建议的代码.但如果你能拿出完整的代码,那么我就能够理解一切。谢谢!

db2dz4w8

db2dz4w81#

我的一个朋友发给我一个更好的,简单的代码来解决这个问题,他已经从互联网上找到
对于业余爱好者(比如我),你必须在“setContentView(R.layout.activity_main);“in“protected void onCreate(Bundle savedInstanceState){”method in yourMainActivity.java

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

顺便说一句,感谢@AbdulKawee的时间和支持,你给了我你的代码。真的很感激:)

0yycz8jy

0yycz8jy2#

你可以用这个

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;
}
8gsdolmq

8gsdolmq3#

Sankha Rathnayake的Kotlin版本的答案:

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

rsl1atfo4#

如果你只想在你的应用中使用它,那么每个应该是全亮度的Activity只需要在它的onCreate()中实现以下代码。
每当您切换活动或切换回其他应用程序-亮度将再次由用户定义
使用Kotlin,你可能想使用apply(),只需要很少的代码:

window.attributes.apply {
    screenBrightness = 1f
}

相关问题