0

我有一个应用程序,通过它我可以手动更改设备的亮度。我想使用传感器来做到这一点。我必须添加什么代码?另外我必须在清单中进行哪些更改?

这是我的代码:

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

1 回答 1

3

您可以使用它来根据更改后的 seekbar 值进行设置

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

如果您需要它一致,那么您可以使用 IHardwareService 接口这是我发现的一个示例

http://www.tutorialforandroid.com/2009/01/changeing-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]);
  }

 }
}
于 2012-10-16T11:01:37.190 回答