|
发表于 2014-2-12 09:53:19
|
显示全部楼层
- //重力感应模块
- sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE); //初始化感应器
- Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //实例化一个重力感应sensor
- SensorEventListener lsn = new SensorEventListener() { //设置重力感应的监听事件
-
- public void onSensorChanged(SensorEvent e) {//当重力感应发生变化时,获取x、y、z三个方向的重力感应加速度值
- // TODO Auto-generated method stub
- x = e.values[SensorManager.DATA_X];
- y = e.values[SensorManager.DATA_Y];
- z = e.values[SensorManager.DATA_Z];
-
- if(mCheckBox.isChecked()){//如果启动重力感应模块,则执行下边的操作
- verticalSeekBar1.setVisibility(View.VISIBLE);//显示seekbar
- if(x < 2){//前进
- sendMsg(upMsg);
- }else if(x > 7){//后退
- sendMsg(downMsg);
- }else if(y <-1){//左转
- sendMsg(leftMsg);
- }else if(y > 1){//右转
- sendMsg(rightMsg);
- }else{//停止
- sendMsg(stopMsg);
- }
- }else
- {
- verticalSeekBar1.setVisibility(View.GONE);//seekbar隐藏
- }
- }
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- // TODO Auto-generated method stub
-
- }
- };
- sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
- }
复制代码
|
|