简体   繁体   中英

changing brightness in android programmatically

when writing code for changing brightness programmatically in android studio, setContentView, getContentResolver, ChangeBright, findViewById and getWindow are in red. it says cannot resolve method. why is that so?

private SeekBar brightbar;
private int brightness;
private ContentResolver Conresolver;
private Window window;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab3);

    brightbar = (SeekBar) findViewById(R.id.ChangeBright);


    Conresolver = getContentResolver();

    window = getWindow();


    brightbar.setMax(255);

    brightbar.setKeyProgressIncrement(1);

    try
    {

        brightness = System.getInt(Conresolver, System.SCREEN_BRIGHTNESS);
    }
    catch (SettingNotFoundException e)
    {

        Log.e("Error", "Cannot access system brightness");
        e.printStackTrace();
    }


    brightbar.setProgress(brightness);


    brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            System.putInt(Conresolver, System.SCREEN_BRIGHTNESS, brightness);

            LayoutParams layoutpars = window.getAttributes();

            layoutpars.screenBrightness = brightness / (float)255;

            window.setAttributes(layoutpars);
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {

        }

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {

            if(progress<=20)
            {

                brightness=20;
            }
            else
            {

                brightness = progress;
            }

您确定您的代码扩展了Activity吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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