[Android实例] Sensor传感器源码的阅读与应用开发简单实例

转自
http://www.apkbus.com/android-804-1-1.html

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


 

 

1.png

2011-4-25 08:22 上传
下载附件 (12.84 KB)



 

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

1
)传感器系统的 java 代码


代码路径: framework/base/core/java/android/hardware
    目录中包含了 Camera Sensor 两部分, 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的一个网站上也能找到!
四。程序代码
1)SensorActivity.java代码


 

  1. package com.sensor;
  2. import android.app.Activity;
  3. import android.hardware.SensorEventListener;
  4. import android.hardware.SensorListener;
  5. import android.hardware.SensorManager;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.widget.TextView;
  9. public class SensorActivity extends Activity implements SensorListener{

  10. final String tag = "SensorActivity";
  11.     SensorManager sm = null;
  12. TextView xViewA = null;
  13. TextView yViewA = null;
  14. TextView zViewA = null;
  15. TextView xViewO = null;
  16. TextView yViewO = null;
  17. TextView zViewO = null;



  18.     /** Called when the activity is first created. */
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.        
  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.        
  33.        
  34.        
  35.     }
  36. @Override
  37. public void onAccuracyChanged(int sensor, int accuracy) {
  38.   // TODO Auto-generated method stub
  39.   Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
  40. }
  41. @Override
  42. public void onSensorChanged(int sensor, float[] values) {
  43.   // TODO Auto-generated method stub
  44.     synchronized (this) {
  45.              Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
  46.              if (sensor == SensorManager.SENSOR_ORIENTATION) {
  47.               xViewO.setText("Orientation X: " + values[0]);
  48.               yViewO.setText("Orientation Y: " + values[1]);
  49.               zViewO.setText("Orientation Z: " + values[2]);
  50.              }
  51.              if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
  52.               xViewA.setText("Accel X: " + values[0]);
  53.               yViewA.setText("Accel Y: " + values[1]);
  54.               zViewA.setText("Accel Z: " + values[2]);
  55.              }           
  56.          }
  57.  
  58. }


  59.   @Override
  60.      protected void onResume() {
  61.          super.onResume();
  62.          sm.registerListener(this,
  63.                  SensorManager.SENSOR_ORIENTATION |
  64.            SensorManager.SENSOR_ACCELEROMETER,
  65.                  SensorManager.SENSOR_DELAY_NORMAL);
  66.      }
  67.     
  68.      @Override
  69.      protected void onStop() {
  70.          sm.unregisterListener(this);
  71.          super.onStop();
  72.      }   
  73.     
  74. }
复制代码

2)main.xml  布局文件(简单的放些TextView)

  1.   <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView 
  8.     android:layout_width="fill_parent"
  9.     android:layout_height="wrap_content"
  10.     android:text="@string/hello"
  11.     />
  12. <TextView 
  13.     android:layout_width="fill_parent"
  14.     android:layout_height="wrap_content"
  15.     android:text="Accelerometer"
  16.     />
  17. <TextView 
  18.     android:layout_width="fill_parent"
  19.     android:layout_height="wrap_content"
  20.     android:text="X Value"
  21.     android:id="@+id/xbox"
  22.     />
  23. <TextView 
  24.     android:layout_width="fill_parent"
  25.     android:layout_height="wrap_content"
  26.     android:text="Y Value"
  27.     android:id="@+id/ybox"
  28.     />
  29. <TextView 
  30.     android:layout_width="fill_parent"
  31.     android:layout_height="wrap_content"
  32.     android:text="Z Value"
  33.     android:id="@+id/zbox"
  34.     />  

  35. <TextView 
  36.     android:layout_width="fill_parent"
  37.     android:layout_height="wrap_content"
  38.     android:text="Orientation"
  39.     />
  40. <TextView 
  41.     android:layout_width="fill_parent"
  42.     android:layout_height="wrap_content"
  43.     android:text="X Value"
  44.     android:id="@+id/xboxo"
  45.     />
  46. <TextView 
  47.     android:layout_width="fill_parent"
  48.     android:layout_height="wrap_content"
  49.     android:text="Y Value"
  50.     android:id="@+id/yboxo"
  51.     />
  52. <TextView 
  53.     android:layout_width="fill_parent"
  54.     android:layout_height="wrap_content"
  55.     android:text="Z Value"
  56.     android:id="@+id/zboxo"
  57.     />   
  58. </LinearLayout>
复制代码

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值