Reduce screen light on activity

325 views Asked by At

In my application, under certain activity must keep the screen always active (classic keepScreenOn).

In order to reduce a little battery consumption, I would like to implement an energy saving system (similar to the default) type:

After 10 seconds of inactivity, the brightness goes to a minimum (without ever sending the activity paused or similar) to tap the brightness returns to normal and so on ...

is possible to implement such a system? there is an automatic way to calculate the idle time? (because otherwise I would create a cooldwon from 10 to 0, to be carried forward to 10 to each user's tap ...)

Thank you very much

4

There are 4 answers

0
Muhammed Refaat On

at first write the following permission in your manifest.xml file

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

then, when the 10 seconds pass, run the following code to reduce the Brightness of the device to it's lower level:

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0); //Set the system brightness
android.view.WindowManager.LayoutParams w_lp = getWindow().getAttributes(); //Get the current window attributes
w_lp.screenBrightness = progress / (float)255; //Set the brightness of this window
getWindow().setAttributes(w_lp); //Apply attribute changes to this window

and in case you need know how to make sure that it waited for 10 seconds without any user activity, implement the above code the following way:

public class YOUR_ACTIVITY extends Activity {

public static int x = 0; // must be static

@Override
protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       run_thread();
}

private void run_thread(){
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            while(x < 10){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                x++;
            }

    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0); //Set the system brightness to it's lowest value
    android.view.WindowManager.LayoutParams w_lp = getWindow().getAttributes(); //Get the current window attributes
    w_lp.screenBrightness = progress / (float)255; //Set the brightness of this window
    getWindow().setAttributes(w_lp); //Apply attribute changes to this window

        }
    });
    thread.start();
}


@Override 
public boolean onTouchEvent(MotionEvent event) {
    x = 0;

    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255); //Set the system brightness back to it's full value
    android.view.WindowManager.LayoutParams w_lp = getWindow().getAttributes(); //Get the current window attributes
    w_lp.screenBrightness = progress / (float)255; //Set the brightness of this window
    getWindow().setAttributes(w_lp); //Apply attribute changes to this window

    return true;
}
2
shkschneider On

I think this is possible using WindowManager.LayoutParams:

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = 1F; // or whatever, but a float
getWindow().setAttributes(layoutParams);

The value ranged from 0 (dark) to 1 (bright). A value below 0 resets the default android value.

0
Don Chakkappan On

Use TimerTask to handle the timings

use

@Override
public void onUserInteraction() {
    // TODO Auto-generated method stub
    super.onUserInteraction();

    Log.d(TRCU.LOG_TAG,"User Interaction : "+userInteractionTimeout);

}

to get the user interaction event

And also you can adjust the brightness using

         // Update brightness Instantly
         WindowManager.LayoutParams lp =((Activity) context).getWindow().getAttributes();
         lp.screenBrightness =mCurrentValue/ 255.0f;

         ((Activity) context).getWindow().setAttributes(lp);
1
A-Droid Tech On

Try Using Settings.System.SCREEN_BRIGHTNESS to set system default brightness as:

android.provider.Settings.System.putInt(getContentResolver(),  
android.provider.Settings.System.SCREEN_BRIGHTNESS,brightness /100.0f); // 0-255 

and add these permission's in manifest :

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