简体   繁体   中英

Android changing brightness using sensor

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;
}
}

You can use this to set according to the changed seekbar value

WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = brightness;
getWindow().setAttributes(layout);

If you need it consistent then you can go with IHardwareService interface Here is an example that i found

http://www.tutorialforandroid.com/2009/01/changing-screen-brightness.html

package com.test;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;

public class Test extends Activity implements SensorEventListener{

 private SensorManager mSensorManager;
 private Sensor mLight;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

   mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
     mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

 }
 @Override
 protected void onResume() {
   mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
  super.onResume();
 }
 @Override
 protected void onPause() {
  mSensorManager.unregisterListener(this);
  super.onPause();
 }
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
   if(sensor.getType() == Sensor.TYPE_LIGHT){
    Log.i("Sensor Changed", "Accuracy :" + accuracy);
   }

 }

 public void onSensorChanged(SensorEvent event) {
  if( event.sensor.getType() == Sensor.TYPE_LIGHT){
   Log.i("Sensor Changed", "onSensor Change :" + event.values[0]);
  }

 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM