android 获取手机中所有的传感器Sensor类使用方法

高仿QQ设置滑动按钮,漂亮简单
http://www.eoeandroid.com/thread-196576-1-1.html

原创:实战db4o数据库01
http://www.eoeandroid.com/thread-196073-1-1.html

Android应用调用浏览器和电话的URI
http://www.eoeandroid.com/thread-195752-1-1.html

 

代码很简单,自己看就能明白,只是为了练习一下Sensor类的使用方法。算是一个练习.

复制代码
package mars.com;
 
import java.util.List;
 
import android.app.Activity;
 
import android.content.Context;
 
import android.hardware.Sensor;
 
import android.hardware.SensorManager;
 
import android.os.Bundle;
 
import android.view.View;
 
import android.view.View.OnClickListener;
 
import android.widget.Button;
 
import android.widget.TextView;
 
public class DemoSensorActivity extends Activity {
 
private Button button;
 
private TextView show;
 
private SensorManager sm;
 
private StringBuffer str;
 
private List<Sensor> allSensors;
 
private Sensor s;
 
@Override
 
public void onCreate(Bundle savedInstanceState) {
 
  super.onCreate(savedInstanceState);
 
  setContentView(R.layout.main);
 
  button = (Button) findViewById(R.id.button);
 
  show = (TextView) findViewById(R.id.show);
 
  button.setOnClickListener(new ButtonListener());
 
  sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
 
  allSensors = sm.getSensorList(Sensor.TYPE_ALL);// 获得传感器列表
 
}
 
class ButtonListener implements OnClickListener {
 
  public void onClick(View v) {
 
   str = new StringBuffer();
 
   str.append("该手机有" + allSensors.size() + "个传感器,分别是:\n");
 
   for (int i = 0; i < allSensors.size(); i++) {
 
    s = allSensors.get(i);
 
    str.append("设备名称:" + s.getName() + "\n");
 
    str.append("设备版本:" + s.getVersion() + "\n");
 
    str.append("通用类型号:" + s.getType() + "\n");
 
    str.append("设备商名称:" + s.getVendor() + "\n");
 
    str.append("传感器功耗:" + s.getPower() + "\n");
 
    str.append("传感器分辨率:" + s.getResolution() + "\n");
 
    str.append("传感器最大量程:" + s.getMaximumRange() + "\n");
 
    switch (s.getType()) {
 
    case Sensor.TYPE_ACCELEROMETER:
 
     str.append(i + "加速度传感器");
 
     break;
 
    case Sensor.TYPE_GYROSCOPE:
 
     str.append(i + "陀螺仪传感器");
 
     break;
 
    case Sensor.TYPE_LIGHT:
 
     str.append(i + "环境光线传感器");
 
     break;
 
    case Sensor.TYPE_MAGNETIC_FIELD:
 
     str.append(i + "电磁场传感器");
 
     break;
 
    case Sensor.TYPE_ORIENTATION:
 
     str.append(i + "方向传感器");
 
     break;
 
    case Sensor.TYPE_PRESSURE:
 
     str.append(i + "压力传感器");
 
     break;
 
    case Sensor.TYPE_PROXIMITY:
 
     str.append(i + "距离传感器");
 
     break;
 
    case Sensor.TYPE_TEMPERATURE:
 
     str.append(i + "温度传感器");
 
     break;
 
    default:
 
     str.append(i + "未知传感器");
 
     break;
 
    }
 
   }
 
   show.setText(str);
 
  }
 
}
 
}
 
复制代码

布局文件

复制代码
<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:layout_width="fill_parent"
 
    android:layout_height="fill_parent"
 
    android:orientation="vertical" >
 
    <Button
 
        android:id="@+id/button"
 
        android:layout_width="fill_parent"
 
        android:layout_height="wrap_content"
 
        android:text="获取传感器" />
 
    <ScrollView
 
        xmlns:android="http://schemas.android.com/apk/res/android"
 
        android:layout_width="fill_parent"
 
        android:layout_height="wrap_content"
 
        android:orientation="vertical" >
 
        <TextView
 
            android:id="@+id/show"
 
            android:layout_width="fill_parent"
 
            android:layout_height="wrap_content" />
 
    </ScrollView>
 
</LinearLayout>
 
复制代码

 

 

posted on   vus520  阅读(3154)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2012年9月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6
点击右上角即可分享
微信分享提示