获取手机中的传感器的步骤为:
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);
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 //这个参数就是设置传感器的采样率 }
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); }
/* * 传感器会通过传感器事件把值传给用户 * 所以,我们的应用程序是被动的,等待传感器把数据传递给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; } }