zoukankan      html  css  js  c++  java
  • 07_重力倾斜度响应


    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.view.Menu;
    import android.widget.TextView;

    public class MainActivity extends Activity  implements SensorEventListener {
        TextView textView;
        StringBuilder builder = new StringBuilder();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            textView = new TextView(this);
            setContentView(textView);

            SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) {
                textView.setText("No accelerometer installed");
            } else {
                Sensor accelerometer = manager.getSensorList(
                        Sensor.TYPE_ACCELEROMETER).get(0);
                if (!manager.registerListener(this, accelerometer,
                        SensorManager.SENSOR_DELAY_GAME)) {
                    textView.setText("Couldn't register sensor listener");
                }
            }
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            builder.setLength(0);
            builder.append("x: ");
            builder.append(event.values[0]);
            builder.append(", y: ");
            builder.append(event.values[1]);
            builder.append(", z: ");
            builder.append(event.values[2]);
            textView.setText(builder.toString());
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // nothing to do here
        }
    }

  • 相关阅读:
    [转帖]译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
    如何建立一个“绑定友好的”usercontrol--wpf
    安卓学习(三)
    安卓学习(二)
    Android学习1
    用伪代码梳理springboot
    用伪代码梳理javaweb从零开始
    用伪代码梳理spring源码
    java如何写出简洁代码
    JAVA修复微信官方SDK支付XXE漏洞
  • 原文地址:https://www.cnblogs.com/xl711436/p/3060421.html
Copyright © 2011-2022 走看看