转自:http://blog.csdn.net/gexueyuan/article/details/6716807


Android系统支持多种传感器。应用到各个层次,有的传感器已经在Android的框架中使用,大多数传感器由应用程序中来使用。

一.Android中支持的传感器类型:

传感器

Java中的名称

本地接口名称

数值

加速度

TYPE_ACCELEROMETER

SENSOR_TYPE_ACCELEROMETER

1

磁场

TYPE_MAGNETIC_FIELD

SENSOR_TYPE_MAGNETIC_FIELD

2

方向

TYPE_ORIENTATION

SENSOR_TYPE_ORIENTATION

3

陀螺仪

TYPE_GYROSCOPE

SENSOR_TYPE_GYROSCOPE

4

光线(亮度)

TYPE_LIGHT

SENSOR_TYPE_LIGHT

5

压力

TYPE_PRESSURE

SENSOR_TYPE_PRESSURE

6

温度

TYPE_TEMPERATURE

SENSOR_TYPE_TEMPERATURE

7

接近

TYPE_PROXIMITY

SENSOR_TYPE_PROXIMITY

8

二.Android系统的代码分布情况:

1)传感器系统的java代码

  代码路径:framework/base/core/java/android/hardware

 目录中包含了CameraSensor两部分,Sensor部分的内容为Sensor*.java文件。


2
)传感器系统的JNI部分

  代码路径: framework/base/core/jni/android_hardware_SensorManager.cpp

 本部分提供了android.hardware.Sensor.Manager类的本质支持。


3
)传感器系统硬件层实现的接口

  头文件路径:hardware/libhardware/include/hardware/sensors.h

  传感器系统的硬件抽象层需要各个系统根据sensors.h中定义的接口去实现

  Sensor部分的内容还包含了底层部分的驱动和硬件抽象层,以及上层对Sensor的调用部


三.Android的Sensor源码解析:

  Android中的Sensor的主要文件为:

  Sensor.java 单一传感器描述文件

  SensorEvent.java 传感器系统的时间类

  SensorEventListener.java 传感器监听事件(是一个接口)

  SensorListener.java 传感器监听(接口)

  SensorManager.java 传感器的核心管理类


  Sensor.java中定义的是传感器常量的一些类型,如public static final TYPE_MAGNETIC_FIELD=2; 等,具体参数参照传感器类型(图一)

  SensorManager.java


public Sensor getDefaultSensor(int type){获得默认的传感器}public List<Sensor> getSensorList(int type) {获得传感器列表}public boolean registerListener(SensorListener listener, int sensors) {return registerListener(listener, sensors, SENSOR_DELAY_NORMAL);} // 注册监听事件public void unregisterListener(SensorListener listener, int sensors) {注销监听事件}

  时间关系,源码不逐一说了,大家自己有下个源码看下,如果没有源码的,给我个邮箱我给大家发这部分代码,直接上个简单的DEMO供大家认识下,好像这块的代码,在IBM的一个网站上也能找到!

四。程序代码

[java] view plain copy
  1. 1)SensorActivity.java代码
  2. packagecom.sensor;
  3. importandroid.app.Activity;
  4. importandroid.hardware.SensorEventListener;
  5. importandroid.hardware.SensorListener;
  6. importandroid.hardware.SensorManager;
  7. importandroid.os.Bundle;
  8. importandroid.util.Log;
  9. importandroid.widget.TextView;
  10. publicclassSensorActivityextendsActivityimplementsSensorListener{
  11. finalStringtag="SensorActivity";
  12. SensorManagersm=null;
  13. TextViewxViewA=null;
  14. TextViewyViewA=null;
  15. TextViewzViewA=null;
  16. TextViewxViewO=null;
  17. TextViewyViewO=null;
  18. TextViewzViewO=null;
  19. /**Calledwhentheactivityisfirstcreated.*/
  20. @Override
  21. publicvoidonCreate(BundlesavedInstanceState){
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. sm=(SensorManager)getSystemService(SENSOR_SERVICE);
  25. xViewA=(TextView)findViewById(R.id.xbox);
  26. yViewA=(TextView)findViewById(R.id.ybox);
  27. zViewA=(TextView)findViewById(R.id.zbox);
  28. xViewO=(TextView)findViewById(R.id.xboxo);
  29. yViewO=(TextView)findViewById(R.id.yboxo);
  30. zViewO=(TextView)findViewById(R.id.zboxo);
  31. }
  32. @Override
  33. publicvoidonAccuracyChanged(intsensor,intaccuracy){
  34. //TODOAuto-generatedmethodstub
  35. Log.d(tag,"onAccuracyChanged:"+sensor+",accuracy:"+accuracy);
  36. }
  37. @Override
  38. publicvoidonSensorChanged(intsensor,float[]values){
  39. //TODOAuto-generatedmethodstub
  40. synchronized(this){
  41. Log.d(tag,"onSensorChanged:"+sensor+",x:"+values[0]+",y:"+values[1]+",z:"+values[2]);
  42. if(sensor==SensorManager.SENSOR_ORIENTATION){
  43. xViewO.setText("OrientationX:"+values[0]);
  44. yViewO.setText("OrientationY:"+values[1]);
  45. zViewO.setText("OrientationZ:"+values[2]);
  46. }
  47. if(sensor==SensorManager.SENSOR_ACCELEROMETER){
  48. xViewA.setText("AccelX:"+values[0]);
  49. yViewA.setText("AccelY:"+values[1]);
  50. zViewA.setText("AccelZ:"+values[2]);
  51. }
  52. }
  53. }
  54. @Override
  55. protectedvoidonResume(){
  56. super.onResume();
  57. sm.registerListener(this,SensorManager.SENSOR_ORIENTATION|
  58. SensorManager.SENSOR_ACCELEROMETER,SensorManager.SENSOR_DELAY_NORMAL);
  59. }
  60. @Override
  61. protectedvoidonStop(){
  62. sm.unregisterListener(this);
  63. super.onStop();
  64. }


[html] view plain copy
  1. 2)main.xml布局文件(简单的放些TextView)
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  4. androidrientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="@string/hello"
  12. />
  13. <TextView
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="Accelerometer"
  17. />
  18. <TextView
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:text="XValue"
  22. android:id="@+id/xbox"
  23. />
  24. <TextView
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:text="YValue"
  28. android:id="@+id/ybox"
  29. />
  30. <TextView
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:text="ZValue"
  34. android:id="@+id/zbox"
  35. />
  36. <TextView
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. android:text="Orientation"
  40. />
  41. <TextView
  42. android:layout_width="fill_parent"
  43. android:layout_height="wrap_content"
  44. android:text="XValue"
  45. android:id="@+id/xboxo"
  46. />
  47. <TextView
  48. android:layout_width="fill_parent"
  49. android:layout_height="wrap_content"
  50. android:text="YValue"
  51. android:id="@+id/yboxo"
  52. />
  53. <TextView
  54. android:layout_width="fill_parent"
  55. android:layout_height="wrap_content"
  56. android:text="ZValue"
  57. android:id="@+id/zboxo"
  58. />
  59. </LinearLayout>


五:在模拟器开发测试Sensor要注意,必须要装个传感器插件,才能看到效果,可能有部分手机硬件驱动是不支持Sensor的,不过市面上流行的品牌手机一般都支持!

抽空首次整理做的教程,有不好的地方,不吝指正!




更多相关文章

  1. Android系统源码下载与编译、刷机--Nexus6实测
  2. Android 编译系统 (一)
  3. 转:Android中如何修改系统时间(应用程序获得系统权限)
  4. android 系统签名
  5. Android系统移植与调试之------->如何修改Android设备添加重启、
  6. Android文件系统的结构及目录用途、操作方法
  7. android文件系统
  8. 在android中利用多线程实现对控件的更新(动态修改文本框中的值)。
  9. 用HTML+JS实现Android闹钟功能,附带Alarm代码分享

随机推荐

  1. android eclipse 报error loading /syste
  2. 【Android】从无到有:手把手一步步教你构
  3. Android(安卓)studio 一直卡在Gradle:Bui
  4. android SharedPreferences 记录数据
  5. Android(安卓)ApiDemos示例解析
  6. Android官方下拉刷新控件 SwipeRefreshLa
  7. Android(安卓)开发第四天
  8. Firebase Crashlytics Sdk接入流程(Androi
  9. Android(安卓)在低版本sdk中没有getSuppo
  10. Android—网络编程