随笔-6  评论-0  文章-0  trackbacks-0

1.传感器事件概念
2.为应用程序注册传感器
3.获取传感器的值和精度
public boolean registerListener (SensorEventListener listener,Sensor sensor,int rate)
样例
package org.marsdroid.sensor()2;
import android.app.Activitity;

public class MainActivity extends Activity{
  //声明一个SensorManager对象
  private SensorManager sensorManager;
  @Override
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //获取SensorManager对象
    sensorManager=(SensorManager)getSystemService(Context.SESOR_SERVICE);
    //获取光线传感器对象
    Sensor lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    //注册传感器
    sensorManager.registerListener(new SensorEventListener(){
      public void onSensorChanged(SensorEvent event){
        float acc=event.accuracy;
        float lux=event.values[0];
        System.out.println("acc---->"+acc);
        System.out.println("lux---->"+lux);
      }
      public void onAccuracyChanged(Sensor sensor,int accuracy){
      }
    },lightSensor,SensorManager.SENSOR_DELAY_NORMAL);
  }
}
------------------------
Http://creativecommons.org/licenses/by-nc-sa/2.5/cn/legalcode

posted on 2012-08-14 14:26 保密 阅读(149) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: