I want to change brightness of screen according to light..
I found links for getting brightness level and setting up brightness..
Change screen brightness onPause (Android app)
How to toggle auto brightness on and off? (not a repeat)
changing screen brightness programmatically in android
Changing screen brightness programmatically (as with the power widget)
But I want to change brightness as per surrounding light.. In short, I want to do same as auto brightness works..
How Can I detect surrounding light?? OR Can I trigger auto brightness functionality??
brightness is having value range between 0 to 255. I want to set brightness level according to surrounding light level which can not be 0 to 255. How can I get ratio between this two.
Help is needed. Thanks
Solution from myself:
I implemented shaking listener from here. I used aboce links also to make it completed.
I created ShakeBrightService.java
public class ShakeBrightService extends Service {
private ShakeDetector shakeBrightDetector = new ShakeDetector();
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
shakeBrightDetector.stop();
shakeBrightDetector.start(this, shakeBrightEventListener);
return super.onStartCommand(intent, flags, startId);
}
private ShakeEventListener shakeBrightEventListener = new ShakeEventListener() {
@Override
public void onShake() {
setAutoBrightness(true);
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void setAutoBrightness(boolean value) {
Toast.makeText(getApplicationContext(), "AutoBrightness : " + value,
Toast.LENGTH_SHORT).show();
if (value) {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} else {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
Intent i = new Intent(getApplicationContext(), MainActivity1.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
}
Fake for refreshing view
MainActivity1.java
public class MainActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main1);
refreshBrightness(getBrightnessLevel());
Thread t = new Thread() {
public void run() {
try {
sleep(10);
} catch (InterruptedException e) {
}
finish();
}
};
t.start();
}
private void refreshBrightness(float brightness) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
if (brightness < 0) {
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
} else {
lp.screenBrightness = brightness;
}
getWindow().setAttributes(lp);
}
int getBrightnessLevel() {
try {
int value = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
// convert brightness level to range 0..1
value = value / 255;
return value;
} catch (SettingNotFoundException e) {
return 0;
}
}
FakeActivity is having theme android:theme="@android:style/Theme.Translucent.NoTitleBar"
, so silently brightness is changed.