以下仅是传感器使用实例,关于传感器的详细简绍请参考《 Android传感器的环境监控 》。 文件1:Main.java import java.util.HashMap; import java.util.List; import java.util.Set; import java.util.Map.Entry;
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.widget.TextView;
public classMain extends ActivityimplementsSensorEventListener{ private TextView tvAccelerometer; private TextView tvMagentic; private TextView tvLight; private TextView tvOrientation; private TextView tvSensors; private TextView tvAccuracy; SensorManager sensorManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获得SensorManager对象 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); tvAccelerometer = (TextView) findViewById(R.id.tvAccelerometer); tvMagentic = (TextView) findViewById(R.id.tvMagentic); tvLight = (TextView) findViewById(R.id.tvLight); tvOrientation = (TextView) findViewById(R.id.tvOrientation); tvSensors = (TextView)findViewById(R.id.tvSensors); tvAccuracy=(TextView)findViewById(R.id.tvAccuracy); // 获得当前手机支持的所有传感器 tvSensors.setText("当前手机支持的所有传感器:\n"); List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL); for(Sensor sensor:sensors) { // 输出当前传感器的名称 tvSensors.append(sensor.getName() + "\n"); } } public voidonResume() { super.onResume(); registerSensorListener(); } public voidonPause() { unRegisterSensorListener(); super.onPause(); } private void registerSensorListener() { // 注册加速度传感器 sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
// 注册磁场传感器 sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_FASTEST);
// 注册光线传感器 sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_FASTEST);
// 注册方向传感器 sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
} private voidunRegisterSensorListener() { sensorManager.unregisterListener(this); } @Override public voidonSensorChanged(SensorEvent event) { // 通过getType方法获得当前传回数据的传感器类型 switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: // 处理加速度传感器传回的数据 String accelerometer = "加速度\n" + "X:" + event.values[0] + " " + "Y:" + event.values[1] + " " + "Z:" + event.values[2]; tvAccelerometer.setText(accelerometer); break; case Sensor.TYPE_LIGHT: // 处理光线传感器传回的数据 tvLight.setText("亮度:" + event.values[0]); break; case Sensor.TYPE_MAGNETIC_FIELD: // 处理磁场传感器传回的数据 String magentic = "磁场\n" + "X:" + event.values[0] + " " + "Y:" + event.values[1] + " " + "Z:" + event.values[2]; tvMagentic.setText(magentic); break; case Sensor.TYPE_ORIENTATION: // 处理方向传感器传回的数据 String orientation = "方向\n" + "X:" + event.values[0] + " " + "Y:" + event.values[1] + " " + "Z:" + event.values[2]; tvOrientation.setText(orientation); break; } } HashMap<String,Integer> hashMapAccuracy=new HashMap<String,Integer>(); StringBuffer strBuffer=new StringBuffer(50); @Override public voidonAccuracyChanged(Sensor sensor, int accuracy) { hashMapAccuracy.put(sensor.getName(), accuracy); Set<Entry<String,Integer>> set=hashMapAccuracy.entrySet(); strBuffer.delete(0, strBuffer.length()); for(Entry<String,Integer>entry:set) { strBuffer.append("传感器"+entry.getKey()+"的精度改变为:"+entry.getValue()+"\n"); } tvAccuracy.setText(strBuffer.toString()); } } 文件2:main.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/tvAccelerometer" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvMagentic" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvLight" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvOrientation" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvSensors" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvAccuracy" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

更多相关文章

  1. Android进度条、自动提示框、下拉框动态数据加载
  2. Android应用程序组件Content Provider在应用程序之间共享数据的
  3. android 摄像头图像数据YUV转Bitmap, 再转Base64
  4. Android 读取网络数据
  5. Android中数据库升级
  6. android直接读取数据库文件

随机推荐

  1. js:值传递引用传递、结构赋值、流程控制
  2. 2021-2-28
  3. IntelliJ IDEA 激活码 2021.02.28日更
  4. Mysql5.7的varchar类型字段建索引竟然失
  5. 给Mysql服务添加防火墙策略,就这么简单
  6. Linux系统强大的查找命令find用法
  7. 生产系统数据丢失恢复案例
  8. 带你重新认识Linux系统的inode
  9. MySQL产品的生命周期
  10. Linux下块设备查看命令lsblk详解