• Sensor(LIGHT)


    package com.example.sensor01;
    
    import java.util.List;
    
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements SensorEventListener {
    
        private SensorManager mSensorManager;
        private TextView textview;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            textview = (TextView) findViewById(R.id.content);
    
            mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            
            //光线感应传感器检测实时的光线强度,光强单位是lux,值越低,越暗
            Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    
            mSensorManager.registerListener(this, sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
    
        }
    
        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
    
            float light = 0;
    
            if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
    
                light = event.values[0];
            }
    
            textview.setText(String.valueOf(light));
    
        }
    }
  • 相关阅读:
    linux top详解
    软件人才必须具备的素质
    合格程序员每天每周每月每年应该做的事
    正则匹配任意字符(包括换行)
    软件测试方案
    LInux进程间的通信方式有哪儿些?
    三网融合
    php路径问题
    xp 安装SATA AHCI驱动
    进程与线程的区别
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/4533815.html
Copyright © 2020-2023  润新知