码迷,mamicode.com
首页 > 其他好文 > 详细

方向传感器

时间:2018-01-31 01:06:13      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:ons   top   class   change   bundle   activity   ted   system   undle   

public class Activity extends ActionBarActivity
implements SensorEventListener{
    SensorManager sensorManager;
    EditText et_sensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et_sensor = (EditText) findViewById(R.id.et_sensor);
        sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);

    }

    @Override
    protected void onStop() {
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;

        StringBuilder sb = new StringBuilder();
        sb.append("rate of x axis : "+values[0]);
        sb.append("\n rate of y axis : " + values[1]);
        sb.append("\n rate of z axis : " + values[2]);
        et_sensor.setText(sb.toString());

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

 

方向传感器

标签:ons   top   class   change   bundle   activity   ted   system   undle   

原文地址:https://www.cnblogs.com/ouyangping/p/8387742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!