I have an app through which I can change the brightness of the device manually. I want to do that using the sensor. What code do I have to add? Also what changes I have to made in the manifest?
Here it is my code:
package com.example.brightnessdemo;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.System;
import android.app.Activity;
import android.content.ContentResolver;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private SeekBar brightbar;
private int brightness;
private ContentResolver cResolver;
private Window window;
TextView tv;
//float BackLightValue = 0.5f;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
brightbar=(SeekBar)findViewById(R.id.seekBar1);
tv=(TextView)findViewById(R.id.textView1);
cResolver=getContentResolver();
window=getWindow();
brightbar.setMax(100);
brightbar.setKeyProgressIncrement(1);
try
{
brightness=System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
brightbar.setProgress(brightness);
int SysBackLightValue = (int)(brightness);
android.provider.Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS,SysBackLightValue);
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
android.view.WindowManager.LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness;
window.setAttributes(layoutpars);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
brightness = progress;
tv.setText(""+brightness);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}