Android中传感器的使用

Android中传感器的使用 

 

  • 传感器类型
    方向、加速表、光线、磁场、临近性、温度等。
  • 采样率
    最快、游戏、普通、用户界面。当应用程序请求特定的采样率时,其实只是对传感器子系统的一个提示,或者一个建议。不保证特定的采样率可用。
  • 准确性
    高、低、中、不可靠。

注意:传感器在Android 1.5 API Level 3 使用了SensorEventListener 代替了SensorListener,使用了SensorEvent来区别以前的int定义。

下面以1.5之后的API做了一个示例:
1)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:id="@+id/o_x" 
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
    />
    <TextView
  android:id="@+id/o_y" 
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
    />
    <TextView
  android:id="@+id/o_z" 
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
    />
    <TextView
  android:id="@+id/a_x" 
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
    />
    <TextView
  android:id="@+id/a_y" 
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
    />
    <TextView
  android:id="@+id/a_z" 
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
    />
</LinearLayout>

2)Activity类
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener{
public static final String TAG = MainActivity.class.getSimpleName();
private SensorManager sm = null;
private TextView oX, oY, oZ, aX, aY, aZ;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   
    sm = (SensorManager)getSystemService(SENSOR_SERVICE);
    oX = (TextView)findViewById(R.id.o_x);
    oY = (TextView)findViewById(R.id.o_y);
    oZ = (TextView)findViewById(R.id.o_z);
    aX = (TextView)findViewById(R.id.a_x);
    aY = (TextView)findViewById(R.id.a_y);
    aZ = (TextView)findViewById(R.id.a_z);
    }
   
    @Override
    protected void onResume() {
    super.onResume();
    sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION ), SensorManager.SENSOR_DELAY_NORMAL);
    sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    }
   
    @Override
    protected void onStop() {
    super.onStop();
    sm.unregisterListener(this);
    }

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
  Log.d(TAG, "onAccuracyChanged: " + sensor.getType() + ", accuracy:" + accuracy);
}

@Override
public void onSensorChanged(SensorEvent event) {
  synchronized (this) {
   float[] values = event.values;
   Log.d(TAG, "onSensorChanged: " + ", type: " + event.sensor.getType() + ", x: " +
     values[0] + ", y: " + values[1] + ", z:" + values[2]);
   if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
    oX.setText("Orientation X:" + values[0]);
    oY.setText("Orientation Y:" + values[1]);
    oZ.setText("Orientation Z:" + values[2]);
   }
   if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
    aX.setText("Accel X:" + values[0]);
    aY.setText("Accel Y:" + values[1]);
    aZ.setText("Accel Z:" + values[2]);
   }
  }
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值