import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.app.Activity;
import android.view.Window;
public class BrightImpl extends Activity {
public void brightset(int brightness ) {
try {
Context context = getApplicationContext();
// Check whether has the write settings permission or not.
boolean settingsCanWrite = hasWriteSettingsPermission(context);
// If do not have then open the Can modify system settings panel.
if(!settingsCanWrite) {
changeScreenBrightness(context, brightness);
}else {
changeScreenBrightness(context, brightness);
}
} catch (Exception e) {}
}
// This function only take effect in real physical android device,
// it can not take effect in android emulator.
private void changeScreenBrightness(Context context, int screenBrightnessValue)
{
// Change the screen brightness change mode to manual.
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
// Apply the screen brightness value to the system, this will change the value in Settings ---> Display ---> Brightness level.
// It will also change the screen brightness for the device.
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, screenBrightnessValue);
//refreshes the screen
/* int br = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = (float) screenBrightnessValue / 255;
getWindow().setAttributes(lp);
*/
Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.screenBrightness = screenBrightnessValue / 255f;
window.setAttributes(layoutParams);
}
// Check whether this app has android write settings permission.
private boolean hasWriteSettingsPermission(Context context)
{
boolean ret = true;
// Get the result from below code.
ret = Settings.System.canWrite(context);
return ret;
}
public boolean isSupported() {
return false;
}
}