android 传感器ceshi,Android代码-传感器-测试手机支持那几种传感
Android代碼----傳感器-----測試手機支持那幾種傳感
一個小小Demo檢測手機支持那幾種傳感:
具體代碼如下:
[Java代碼]DemoSensorActivity.java
package com.example.testsensor;
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 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代碼]main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="獲取傳感器" />
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
總結
以上是生活随笔為你收集整理的android 传感器ceshi,Android代码-传感器-测试手机支持那几种传感的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何避免投资风险过高的基金?把握这五点
- 下一篇: 5年期的存款,为什么会被银行冷落?