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.