android - 如何获取光传感器的具体值

标签 android sensors light

我刚开始学习android。我开发了一个名为 Android Light Sensor 的程序来测量光的强度。这是我的代码:

    package com.AndroidLightSensor;

import com.example.andriodlightsensor.R;

import android.os.Bundle;
import android.app.Activity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndriodLightSensorActivity extends Activity {

    ProgressBar lightMeter;
     TextView textMax, textReading;
     float counter;
     Button read;
     TextView display;

        /** Called when the activity is first created. */

     @Override
        public void onCreate(Bundle savedInstanceState) {
         counter = 0;
         read = (Button) findViewById(R.id.bStart);
         display = (TextView) findViewById(R.id.tvDisplay);

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            lightMeter = (ProgressBar)findViewById(R.id.lightmeter);
            textMax = (TextView)findViewById(R.id.max);
            textReading = (TextView)findViewById(R.id.reading);

            SensorManager sensorManager 
            = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
            Sensor lightSensor 
            = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

            if (lightSensor == null){
             Toast.makeText(AndriodLightSensorActivity.this, 
               "No Light Sensor! quit-", 
               Toast.LENGTH_LONG).show();
            }else{
             float max =  lightSensor.getMaximumRange();
             lightMeter.setMax((int)max);
             textMax.setText("Max Reading(Lux): " + String.valueOf(max));

             sensorManager.registerListener(lightSensorEventListener, 
               lightSensor, 
               SensorManager.SENSOR_DELAY_NORMAL);

            }
        }

     SensorEventListener lightSensorEventListener
        = new SensorEventListener(){

      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
       // TODO Auto-generated method stub

      }

      @Override
      public void onSensorChanged(SensorEvent event) {
       // TODO Auto-generated method stub
       if(event.sensor.getType()==Sensor.TYPE_LIGHT){
        final float currentReading = event.values[0];
        lightMeter.setProgress((int)currentReading);
        textReading.setText("Current Reading(Lux): " + String.valueOf(currentReading));
        read.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                display.setText("" + String.valueOf(currentReading));
            }
        });

       }
      }

        };
    }

此外,xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tvDisplay"

    />
<ProgressBar
    android:id="@+id/lightmeter"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="80dp"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="100"
    android:progress="0"
    />
<TextView 
    android:id="@+id/max"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    />
<TextView 
    android:id="@+id/reading"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 

    />
<Button 
    android:layout_width="250dp" 
    android:layout_height="wrap_content"
    android:text="Start"  
    android:layout_y="249dp"  
    android:textSize="30dp"
    android:onClick="onButtonDown"
    android:id="@+id/bStart" 
    ></Button>

</LinearLayout>

我想在每次按下按钮时获取当前值; i:e 值不应不断变化(就像在秒表中一样,但更新后的值应替换前一个)。在 Eclipse 中,它没有显示任何错误,但是当我在我的设备上运行时显示“不幸的是,Android 光传感器已停止。” 请帮忙!!

最佳答案

有一个明显的错误。
如果在 setContentView(R.layout.main); 之前使用了 findViewById,则返回的值为 null。
当您尝试使用它们时,您会收到错误消息。

将这两行放在 setContentView(R.layout.main); 之后

read = (Button) findViewById(R.id.bStart);
display = (TextView) findViewById(R.id.tvDisplay);

关于android - 如何获取光传感器的具体值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609370/

相关文章:

android - 来自广播接收器的 Toast 消息

filter - 卡尔曼滤波器与指数滤波器

android - 需要根据 Sensor.TYPE_ORIENTATION 数据计算旋转矢量

java - 为什么我的 native C++ 代码在 Android 上的运行速度比 Java 慢得多?

java - 如何在方法中插入 id 作为参数?

android - 工具栏与 LinearLayout 重叠

android - 使用红外激光传感器 Nexus 6P 和 5X 测量距离

Three.js:如何在运行时添加和删除灯光?

javascript - 如何执行脚本一次直到 if 变为 false? ( Node .JS)

unity-game-engine - Unity2D 让光线照射到物体后面