獲取手機中的傳感器的步驟為:
獲取SensorManager對象
執行SensorManager對象的getDefaultSensor()方法獲取Sensor對象,這裡getDefaultSensor()方法要傳遞一個參數,指定具體的傳感器類型
獲取Sensor對象中的各種屬性
下面給出獲取手機內傳感器的實現代碼:
[java]
Log.d("BruceZhang", "以下是你的手機裡的傳感器:");
List<Sensor> sensors=sensorManager.getSensorList(Sensor.TYPE_ALL);
Log.d("BruceZhang", "--->");
for(Sensor sensor: sensors){
Log.d("BruceZhang", sensor.getName());
}
//通過使用getDefaultSensor方法獲得一個特定的傳感器
/ Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
在知道怎麼獲取傳感器之後,我們需要使用程序獲取傳感器的值,下面給出實例代碼:
[java]
SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
/*
* SensorEvent event
* 一旦傳感器感知外界條件發生變化,就會把相應的數據傳遞給這個對象
* 一般會把這個方法放在onCreate裡,不用手動的去獲取
*/
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float auc=event.accuracy;
//這裡的第一個元素代表的是光線的強度
float lux=event.values[0];
Log.d("BruceZhang", auc+"");
Log.d("BruceZhang", lux+"");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
//SensorManager.SENSOR_DELAY_NORMAL
//這個參數就是設置傳感器的采樣率
}
傳感器有一系列的屬性:
1、傳感器的采樣率
a) SENSOR_DELAY_NOMAL (200000微秒)
b) SENSOR_DELAY_UI (60000微秒)
c) SENSOR_DELAY_GAME (20000微秒)
d) SENSOR_DELAY_FASTEST (0微秒)
2、傳感器的屬性
a) 傳感器的名稱
b) 傳感器制造商
c) 傳感器功率
d) 傳感器的resulotion
獲取這些屬性的方法也很簡單,代碼如下:
[java]
SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
//傳感器的名字
String sensorName=event.sensor.getName();
//傳感器制造商的名字
String sensorVendor=event.sensor.getVendor();
//傳感器的分辨率,傳感器所能識別的最小的精度是多少
float sensorRevolution=event.sensor.getResolution();
//傳感器的功率
float sensorPower=event.sensor.getPower();
//傳感器取得數據的時間是多少
double timeStamp=event.timestamp;
//打印數據
Log.d("BruceZhang", sensorName+"");
Log.d("BruceZhang", sensorVendor+"");
Log.d("BruceZhang", sensorRevolution+"");
Log.d("BruceZhang", sensorPower+"");
Log.d("BruceZhang", timeStamp+"");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
下面實現的這個實例實現了上述內容的綜合:
實例的完整代碼如下:
[java]
/*
* 傳感器會通過傳感器事件把值傳給用戶
* 所以,我們的應用程序是被動的,等待傳感器把數據傳遞給SensorEvent對象
*/
public class MainActivity extends Activity {
private Button button;
private Button button2;
private Button button3;
private SensorManager sensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到SensorManager對象
sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
button=(Button)findViewById(R.id.sensorButton);
button2=(Button)findViewById(R.id.lightButton);
button3=(Button)findViewById(R.id.lightButton2);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
//傳感器的名字
String sensorName=event.sensor.getName();
//傳感器制造商的名字
String sensorVendor=event.sensor.getVendor();
//傳感器的分辨率,傳感器所能識別的最小的精度是多少
float sensorRevolution=event.sensor.getResolution();
//傳感器的功率
float sensorPower=event.sensor.getPower();
//傳感器取得數據的時間是多少
double timeStamp=event.timestamp;
//打印數據
Log.d("BruceZhang", sensorName+"");
Log.d("BruceZhang", sensorVendor+"");
Log.d("BruceZhang", sensorRevolution+"");
Log.d("BruceZhang", sensorPower+"");
Log.d("BruceZhang", timeStamp+"");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
/*
* SensorEvent event
* 一旦傳感器感知外界條件發生變化,就會把相應的數據傳遞給這個對象
* 一般會把這個方法放在onCreate裡,不用手動的去獲取
*/
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float auc=event.accuracy;
//這裡的第一個元素代表的是光線的強度
float lux=event.values[0];
Log.d("BruceZhang", auc+"");
Log.d("BruceZhang", lux+"");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
//SensorManager.SENSOR_DELAY_NORMAL
//這個參數就是設置傳感器的采樣率
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("BruceZhang", "以下是你的手機裡的傳感器:");
List<Sensor> sensors=sensorManager.getSensorList(Sensor.TYPE_ALL);
Log.d("BruceZhang", "--->");
for(Sensor sensor: sensors){
Log.d("BruceZhang", sensor.getName());
}
//通過使用getDefaultSensor方法獲得一個特定的傳感器
// Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}