传感器---光传感
2013-03-23 23:18
281 查看
一个简单的小Demo 测试手机光传感:
具体代码如下:
[java代码]Sample.java
[xml代码]main.xml
string.xml
具体代码如下:
[java代码]Sample.java
package com.example.test_sensor_light; import org.openintents.sensorsimulator.hardware.Sensor; import android.app.Activity; import android.hardware.SensorManager; import android.os.Bundle; import android.widget.TextView; public class Sample extends Activity implements android.hardware.SensorEventListener { private TextView myTextView1; private SensorManager mySensorManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myTextView1 = (TextView) findViewById(R.id.myTextView1); mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } @Override protected void onResume() { mySensorManager.registerListener( this, mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME ); super.onResume(); } @Override protected void onStop() { // TODO Auto-generated method stub mySensorManager.unregisterListener(this); super.onStop(); } @Override protected void onPause() { mySensorManager.unregisterListener(this); super.onPause(); } @Override public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(android.hardware.SensorEvent event) { // TODO Auto-generated method stub float[] values = event.values; int sensorType = event.sensor.TYPE_LIGHT; if (sensorType == Sensor.TYPE_LIGHT) { myTextView1.setText("当前光的强度为:"+values[0]); } } }
[xml代码]main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/myTextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
string.xml
<resources> <string name="app_name">Sample</string> <string name="title">光传感器</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> </resources>
相关文章推荐
- (传感器网络年会)李建中:大数据与传感网(大数据计算的挑战与机遇)
- Atitit 传感器之道 1. 视觉传感器 摄像头 1 1.1. 一、光线传感器: 1 1.2. 二、距离传感器: 1 1.3. 第一种是震动传感器。 4 1.4. 第二种是声响传感
- 传感器---方向传感
- 传感器系列之4.1振动传感实验
- 传感器-----测试手机支持那几种传感
- 选择湿度传感器注意的若干问题
- 机械加工过程的传感检测技术
- 变送器和传感器有什么区别和联系?
- 【原创】一个传感器电压值采集记录分析的小程序
- 4-20 ma电流环原理分析 传感器
- 传感器的技术参数
- Android方向传感器实践——自己动手做指南针
- STM32的内部温度传感器
- 称重传感器知识:型号,认证,性能与选择
- android 传感器的使用
- Android游戏开发之传感器
- Android下调用传感器
- 【Android游戏开发十八】解放手指,利用传感器开发游戏!(本文讲解在SurfaceView中用重力传感器控制圆球的各方向移动)
- Andorid 传感器
- 情感传感器:手机发微博将可带情绪标记