Android使用传感器改变亮度

时间:2012-10-16 10:50:01

标签: android android-sensors

我有一个应用程序,通过它我可以手动更改设备的亮度。我想用传感器做到这一点。我需要添加什么代码? 我还要在清单中做出哪些改变?

这是我的代码:

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

1 个答案:

答案 0 :(得分:2)

您可以根据更改的搜索栏值

进行设置
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = brightness;
getWindow().setAttributes(layout);

如果你需要它一致,那么你可以去 IHardwareService接口 这是我找到的一个例子

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

 }
}