zoukankan      html  css  js  c++  java
  • Android 摇一摇监听实现

    package com.loaderman.androiddemo;
    
    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    
    
    
    public class ShakeUtils implements SensorEventListener {
        public ShakeUtils(Context context) {
            mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        }
    
        public void setOnShakeListener(OnShakeListener onShakeListener) {
            mOnShakeListener = onShakeListener;
        }
    
        public void onResume() {
            mSensorManager.registerListener(this,
                    mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    
        public void onPause() {
            mSensorManager.unregisterListener(this);
        }
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            int sensorType = event.sensor.getType();
            //values[0]:X轴,values[1]:Y轴,values[2]:Z轴
            float[] values = event.values;
            if (sensorType == Sensor.TYPE_ACCELEROMETER) {
                //这里可以调节摇一摇的灵敏度
                if ((Math.abs(values[0]) > SENSOR_VALUE || Math.abs(values[1]) > SENSOR_VALUE || Math.abs(values[2]) > SENSOR_VALUE)) {
                    System.out.println("sensor value == " + " " + values[0] + " " + values[1] + " " + values[2]);
                    if (null != mOnShakeListener) {
                        mOnShakeListener.onShake();
                    }
                }
            }
        }
    
        public interface OnShakeListener {
            public void onShake();
        }
    
        private SensorManager mSensorManager = null;
        private OnShakeListener mOnShakeListener = null;
        private static final int SENSOR_VALUE = 14;
    }
    package com.loaderman.androiddemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        ShakeUtils mShakeUtils;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mShakeUtils = new ShakeUtils( this );
            mShakeUtils.setOnShakeListener(new ShakeUtils.OnShakeListener() {
                @Override
                public void onShake() {
                    System.out.println("摇一摇");
                }
            });
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mShakeUtils.onResume();
        }
        @Override
        protected void onPause() {
            super.onPause();
            mShakeUtils.onPause( );
        }
    }
  • 相关阅读:
    HDU 2376 树形dp|树上任意两点距离和的平均值
    POJ2342 树形dp
    Codeforces 699D Fix a Tree 并查集
    第七届山东省ACM省赛
    [转]override和new的区别
    [转]C#的各种命名规范
    [转]DotNetBar.Bar作为容器使用的方法及Text更新原理
    [转]WPF: ShowDialog() 切换到其他应用窗口后,再切换回来无法让子窗口总在最上方
    c#校验主程序本身的MD5
    [转]WinForm登陆:窗体间的数据传递
  • 原文地址:https://www.cnblogs.com/loaderman/p/10342847.html
Copyright © 2011-2022 走看看