[Android实例] Sensor传感器源码的阅读与应用开发简单实例
2013-01-14 01:02
871 查看
转自
http://www.apkbus.com/android-804-1-1.html
[align=left]Android系统支持多种传感器。应用到各个层次,有的传感器已经在Android的框架中使用,大多数传感器由应用程序中来使用。[/align]
[align=left]一.Android中支持的传感器类型:[/align]
![](https://oscdn.geek-share.com/Uploads/Images/Content/201301/4fb5eccfb54c9b83cc22d990e0493eb9.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中定义的接口去实现
[align=left]Sensor部分的内容还包含了底层部分的驱动和硬件抽象层,以及上层对Sensor的调用部[/align]
[align=left]三.Android的Sensor源码解析:[/align]
[align=left] Android中的Sensor的主要文件为:[/align]
[align=left] Sensor.java 单一传感器描述文件[/align]
[align=left] SensorEvent.java 传感器系统的时间类[/align]
[align=left] SensorEventListener.java 传感器监听事件(是一个接口)[/align]
[align=left] SensorListener.java 传感器监听(接口)[/align]
[align=left] SensorManager.java 传感器的核心管理类[/align]
[align=left] Sensor.java中定义的是传感器常量的一些类型,如public static final TYPE_MAGNETIC_FIELD=2;[/align]
[align=left] 等,具体参数参照传感器类型(图一)[/align]
[align=left] SensorManager.java [/align]
[align=left] public Sensor getDefaultSensor(int type){获得默认的传感器}[/align]
[align=left] public List<Sensor> getSensorList(int type) {获得传感器列表}[/align]
public boolean registerListener(SensorListener listener, int sensors) {
return registerListener(listener, sensors, SENSOR_DELAY_NORMAL);
} 注册监听事件
[align=left] public void unregisterListener(SensorListener listener, int sensors) {注销监听事件}[/align]
[align=left] 时间关系,源码不逐一说了,大家自己有下个源码看下,如果没有源码的,给我个邮箱我给大家发这部分代码,直接上个简单的DEMO供大家认识下,好像这块的代码,在IBM的一个网站上也能找到![/align]
[align=left]四。程序代码[/align]
[align=left]1)SensorActivity.java代码[/align]
package com.sensor;
import android.app.Activity;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SensorActivity extends Activity implements SensorListener{
final String tag = "SensorActivity";
SensorManager sm = null;
TextView xViewA = null;
TextView yViewA = null;
TextView zViewA = null;
TextView xViewO = null;
TextView yViewO = null;
TextView zViewO = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
xViewA = (TextView) findViewById(R.id.xbox);
yViewA = (TextView) findViewById(R.id.ybox);
zViewA = (TextView) findViewById(R.id.zbox);
xViewO = (TextView) findViewById(R.id.xboxo);
yViewO = (TextView) findViewById(R.id.yboxo);
zViewO = (TextView) findViewById(R.id.zboxo);
}
@Override
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}
@Override
public void onSensorChanged(int sensor, float[] values) {
// TODO Auto-generated method stub
synchronized (this) {
Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor == SensorManager.SENSOR_ORIENTATION) {
xViewO.setText("Orientation X: " + values[0]);
yViewO.setText("Orientation Y: " + values[1]);
zViewO.setText("Orientation Z: " + values[2]);
}
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
xViewA.setText("Accel X: " + values[0]);
yViewA.setText("Accel Y: " + values[1]);
zViewA.setText("Accel Z: " + values[2]);
}
}
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this,
SensorManager.SENSOR_ORIENTATION |
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
sm.unregisterListener(this);
super.onStop();
}
}
复制代码
2)main.xml 布局文件(简单的放些TextView)
<?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:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Accelerometer"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X Value"
android:id="@+id/xbox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y Value"
android:id="@+id/ybox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z Value"
android:id="@+id/zbox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Orientation"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X Value"
android:id="@+id/xboxo"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y Value"
android:id="@+id/yboxo"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z Value"
android:id="@+id/zboxo"
/>
</LinearLayout>
复制代码
五:在模拟器开发测试Sensor要注意,必须要装个传感器插件,才能看到效果,可能有部分手机硬件驱动是不支持Sensor的,不过市面上流行的品牌手机一般都支持!
http://www.apkbus.com/android-804-1-1.html
[align=left]Android系统支持多种传感器。应用到各个层次,有的传感器已经在Android的框架中使用,大多数传感器由应用程序中来使用。[/align]
[align=left]一.Android中支持的传感器类型:[/align]
![](https://oscdn.geek-share.com/Uploads/Images/Content/201301/4fb5eccfb54c9b83cc22d990e0493eb9.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中定义的接口去实现
[align=left]Sensor部分的内容还包含了底层部分的驱动和硬件抽象层,以及上层对Sensor的调用部[/align]
[align=left]三.Android的Sensor源码解析:[/align]
[align=left] Android中的Sensor的主要文件为:[/align]
[align=left] Sensor.java 单一传感器描述文件[/align]
[align=left] SensorEvent.java 传感器系统的时间类[/align]
[align=left] SensorEventListener.java 传感器监听事件(是一个接口)[/align]
[align=left] SensorListener.java 传感器监听(接口)[/align]
[align=left] SensorManager.java 传感器的核心管理类[/align]
[align=left] Sensor.java中定义的是传感器常量的一些类型,如public static final TYPE_MAGNETIC_FIELD=2;[/align]
[align=left] 等,具体参数参照传感器类型(图一)[/align]
[align=left] SensorManager.java [/align]
[align=left] public Sensor getDefaultSensor(int type){获得默认的传感器}[/align]
[align=left] public List<Sensor> getSensorList(int type) {获得传感器列表}[/align]
public boolean registerListener(SensorListener listener, int sensors) {
return registerListener(listener, sensors, SENSOR_DELAY_NORMAL);
} 注册监听事件
[align=left] public void unregisterListener(SensorListener listener, int sensors) {注销监听事件}[/align]
[align=left] 时间关系,源码不逐一说了,大家自己有下个源码看下,如果没有源码的,给我个邮箱我给大家发这部分代码,直接上个简单的DEMO供大家认识下,好像这块的代码,在IBM的一个网站上也能找到![/align]
[align=left]四。程序代码[/align]
[align=left]1)SensorActivity.java代码[/align]
package com.sensor;
import android.app.Activity;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SensorActivity extends Activity implements SensorListener{
final String tag = "SensorActivity";
SensorManager sm = null;
TextView xViewA = null;
TextView yViewA = null;
TextView zViewA = null;
TextView xViewO = null;
TextView yViewO = null;
TextView zViewO = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
xViewA = (TextView) findViewById(R.id.xbox);
yViewA = (TextView) findViewById(R.id.ybox);
zViewA = (TextView) findViewById(R.id.zbox);
xViewO = (TextView) findViewById(R.id.xboxo);
yViewO = (TextView) findViewById(R.id.yboxo);
zViewO = (TextView) findViewById(R.id.zboxo);
}
@Override
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}
@Override
public void onSensorChanged(int sensor, float[] values) {
// TODO Auto-generated method stub
synchronized (this) {
Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor == SensorManager.SENSOR_ORIENTATION) {
xViewO.setText("Orientation X: " + values[0]);
yViewO.setText("Orientation Y: " + values[1]);
zViewO.setText("Orientation Z: " + values[2]);
}
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
xViewA.setText("Accel X: " + values[0]);
yViewA.setText("Accel Y: " + values[1]);
zViewA.setText("Accel Z: " + values[2]);
}
}
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this,
SensorManager.SENSOR_ORIENTATION |
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
sm.unregisterListener(this);
super.onStop();
}
}
复制代码
2)main.xml 布局文件(简单的放些TextView)
<?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:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Accelerometer"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X Value"
android:id="@+id/xbox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y Value"
android:id="@+id/ybox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z Value"
android:id="@+id/zbox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Orientation"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X Value"
android:id="@+id/xboxo"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y Value"
android:id="@+id/yboxo"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z Value"
android:id="@+id/zboxo"
/>
</LinearLayout>
复制代码
五:在模拟器开发测试Sensor要注意,必须要装个传感器插件,才能看到效果,可能有部分手机硬件驱动是不支持Sensor的,不过市面上流行的品牌手机一般都支持!
相关文章推荐
- Sensor传感器源码的阅读与应用开发简单实例
- Sensor传感器源码的阅读与应用开发…
- Sensor传感器源码的阅读与应用开发…
- Android应用开发提高篇(3)-----传感器(Sensor)编程
- Android应用开发提高篇(3)-----传感器(Sensor)编程(转)
- Android 开发程序锁应用简单实例
- 实例源码--Android简单团购应用源码
- Android WIFI应用简单开发实例
- Android应用开发提高篇(3)-----传感器(Sensor)编程
- android开发笔记之高级主题—传感器的简单应用
- Android 插件化开发——应用的启动过程源码解析(简单清晰)
- android应用开发全程实录-用户界面部分章节-你真的会用最简单的TextView么?
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- java图像界面开发简单实例-Graphics2D、Rectangle2D、Ellipse2D、Line2D的简单应用
- Android应用开发揭秘的第16个程序5_11_ShaderImg修改版和高仿版的源码注释
- 从Android源码的角度理解应用开发(1)-Touch机制
- android 应用开发-实现简单的音乐播放功能
- Android游戏开发教程之四:Canvas与Paint的应用实例
- 推荐12个亲测Android开发源码(包括应用、游戏、效果等等)
- 【Android应用开发】-屏幕截图功能--截取全屏,无需Root(附源码)