使用Android的传感器亮度变化 [英] Android changing brightness using sensor

查看:362
本文介绍了使用Android的传感器亮度变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,通过它我可以手动更改设备的亮度。我想这样做,使用传感器。什么code我必须补充?
还什么样的变化我必须在manifest做?

这是我的code:

 包com.example.brightnessdemo;进口android.os.Bundle;
进口android.provider.Settings;
进口android.provider.Settings.SettingNotFoundException;
进口android.provider.Settings.System;
进口android.app.Activity;
进口android.content.ContentResolver;
进口android.view.Menu;
进口android.view.Window;
进口android.view.WindowManager;
进口android.widget.RelativeLayout.LayoutParams;
进口android.widget.SeekBar;
进口android.widget.SeekBar.OnSeekBarChangeListener;
进口android.widget.TextView;
进口android.widget.Toast;公共类MainActivity扩展活动
{私人搜索栏brightbar;
私人诠释亮度;
私人ContentResolver的cResolver;
私人窗口窗口;TextView的电视;
//浮动BackLightValue = 0.5F;
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    brightbar =(搜索栏)findViewById(R.id.seekBar1);
    电视=(的TextView)findViewById(R.id.textView1);    cResolver = getContentResolver();
    窗口= getWindow();    brightbar.setMax(100);
    brightbar.setKeyProgressIncrement(1);    尝试
    {
        亮度= System.getInt(cResolver,System.SCREEN_BRIGHTNESS);
    }
    赶上(SettingNotFoundException E)
    {
        // TODO自动生成catch块
        e.printStackTrace();
    }
    brightbar.setProgress(亮度);    INT SysBackLightValue =(int)的(亮度);
    android.provider.Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS,SysBackLightValue);    brightbar.setOnSeekBarChangeListener(新OnSeekBarChangeListener()
    {        @覆盖
        公共无效onStopTrackingTouch(搜索栏搜索栏)
        {
            System.putInt(cResolver,System.SCREEN_BRIGHTNESS,亮度);
            android.view.WindowManager.LayoutParams layoutpars = window.getAttributes();
            layoutpars.screenBrightness =亮度;
            window.setAttributes(layoutpars);        }        @覆盖
        公共无效onStartTrackingTouch(搜索栏搜索栏)
        {
            // TODO自动生成方法存根        }        @覆盖
        公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔FROMUSER)
        {
               亮度=进展情况;
               tv.setText(+亮度);        }
    });}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}
}


解决方案

您可以使用此根据变化的搜索栏值来设置

  WindowManager.LayoutParams布局= getWindow()的getAttributes();
layout.screenBrightness =亮度;
。getWindow()setAttributes(布局);

如果你需要它保持一致,那么你可以去用
IHardwareService接口
下面是我发现的例子。

http://www.tutorialforandroid.com/2009/01/改变 - 屏幕brightness.html

  com.test包;进口android.app.Activity;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
进口android.os.Bundle;
进口android.util.Log;公共类测试扩展活动实现SensorEventListener { 私人的SensorManager mSensorManager;
 私人传感器mLight; @覆盖
 公共无效的onCreate(捆绑savedInstanceState){
  super.onCreate(savedInstanceState);
  的setContentView(R.layout.main);   mSensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
     mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); }
 @覆盖
 保护无效onResume(){
   mSensorManager.registerListener(这一点,mLight,SensorManager.SENSOR_DELAY_NORMAL);
  super.onResume();
 }
 @覆盖
 保护无效的onPause(){
  mSensorManager.unregisterListener(本);
  super.onPause();
 }
 公共无效onAccuracyChanged(传感器传感器,精度INT){
   如果(sensor.getType()== Sensor.TYPE_LIGHT){
    Log.i(传感器已更改,精度:+精度);
   } } 公共无效onSensorChanged(SensorEvent事件){
  如果(event.sensor.getType()== Sensor.TYPE_LIGHT){
   Log.i(传感器已更改,onSensor更改:+ event.values​​ [0]);
  } }
}

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

 }
}

这篇关于使用Android的传感器亮度变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆