zoukankan      html  css  js  c++  java
  • 监听手机晃动(摇一摇)SensorEventListener

    1. import android.content.Context;  
    2. import android.hardware.Sensor;  
    3. import android.hardware.SensorEvent;  
    4. import android.hardware.SensorEventListener;  
    5. import android.hardware.SensorManager;  
    6.   
    7. /** 
    8.  *  
    9.  * 一个检测手机摇晃的监听器 
    10.  * @author fuzhengchao 
    11.  * 
    12.  */  
    13. public class ShakeListener implements SensorEventListener {  
    14.  //速度阈值,当摇晃速度达到这值后产生作用  
    15.  private static final int SPEED_SHRESHOLD = 4000;  
    16.  //两次检测的时间间隔  
    17.  private static final int UPTATE_INTERVAL_TIME = 70;  
    18.    
    19.  //传感器管理器  
    20.  private SensorManager sensorManager;  
    21.  //传感器  
    22.  private Sensor sensor;  
    23.  //重力感应监听器  
    24.  private OnShakeListener onShakeListener;  
    25.  //上下文  
    26.  private Context context;  
    27.  //手机上一个位置时重力感应坐标  
    28.  private float lastX;  
    29.  private float lastY;  
    30.  private float lastZ;  
    31.    
    32.  //上次检测时间  
    33.  private long lastUpdateTime;  
    34.   
    35.  //构造器  
    36.  public ShakeListener(Context c) {  
    37.   //获得监听对象  
    38.   context = c;  
    39.   start();  
    40.  }  
    41.    
    42.  //开始  
    43.  public void start() {  
    44.   //获得传感器管理器  
    45.   sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);   
    46.   if(sensorManager != null) {  
    47.    //获得重力传感器  
    48.    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
    49.   }  
    50.   //注册  
    51.   if(sensor != null) {  
    52.    sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);  
    53.   }  
    54.     
    55.  }  
    56.    
    57.  //停止检测  
    58.  public void stop() {  
    59.   sensorManager.unregisterListener(this);  
    60.  }  
    61.    
    62.  //摇晃监听接口  
    63.  public interface OnShakeListener {  
    64.   public void onShake();  
    65.  }  
    66.    
    67.  //设置重力感应监听器  
    68.  public void setOnShakeListener(OnShakeListener listener) {  
    69.   onShakeListener = listener;  
    70.  }  
    71.    
    72.    
    73.  //重力感应器感应获得变化数据  
    74.  public void onSensorChanged(SensorEvent event) {  
    75.   //现在检测时间  
    76.   long currentUpdateTime = System.currentTimeMillis();  
    77.   //两次检测的时间间隔  
    78.   long timeInterval = currentUpdateTime - lastUpdateTime;    
    79.   //判断是否达到了检测时间间隔  
    80.   if(timeInterval < UPTATE_INTERVAL_TIME)   
    81.    return;  
    82.   //现在的时间变成last时间  
    83.   lastUpdateTime = currentUpdateTime;  
    84.     
    85.   //获得x,y,z坐标  
    86.   float x = event.values[0];  
    87.   float y = event.values[1];  
    88.   float z = event.values[2];  
    89.     
    90.   //获得x,y,z的变化值  
    91.   float deltaX = x - lastX;  
    92.   float deltaY = y - lastY;  
    93.   float deltaZ = z - lastZ;  
    94.     
    95.   //将现在的坐标变成last坐标  
    96.   lastX = x;  
    97.   lastY = y;  
    98.   lastZ = z;  
    99.     
    100.   double speed = Math.sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ)/timeInterval * 10000;  
    101.   //达到速度阀值,发出提示  
    102.   if(speed >= SPEED_SHRESHOLD)  
    103.    onShakeListener.onShake();  
    104.  }  
    105.    
    106.  public void onAccuracyChanged(Sensor sensor, int accuracy) {  
    107.     
    108.  }  
    109.   
    110. }  



    用法: 

    Java代码  
    1. ShakeListener shakeListener = new ShakeListener(this);//创建一个对象  
    2. shakeListener.setOnShakeListener(new OnShakeListener(){//调用setOnShakeListener方法进行监听  
    3.   
    4. public void onShake() {  
    5.     //对手机摇晃后的处理(如换歌曲,换图片,震动……)  
    6.     //onVibrator();  
    7. }  
    8.   
    9. });  



    //震动 

    Java代码 
    1. private void onVibrator() {  
    2.   Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);  
    3.   if (vibrator == null) {  
    4.    Vibrator localVibrator = (Vibrator) context.getApplicationContext()  
    5.      .getSystemService("vibrator");  
    6.    vibrator = localVibrator;  
    7.   }  
    8.   vibrator.vibrate(100L);  
    9.  }  



    摇一摇精简代码实现 
    http://blog.sina.com.cn/s/blog_66cfbaa5010120w8.html 

    Android中类似于奇虎360手机卫士中摇一摇效果实现 
    http://blog.csdn.net/nono_love_lilith/article/details/7554341

  • 相关阅读:
    【算法】Kruskal算法(解决最小生成树问题) 含代码实现
    POJ 1182 食物链 (并查集解法)(详细注释)
    APICloud关闭Key Building Resolve
    ubuntu配置国内源
    缓存穿透、缓存击穿、缓存雪崩概念及解决方案
    POST请求和GET请求的区别
    ibatis 中#和 $ 符号的区别
    自动装箱和自动拆箱理解
    回文串算法说明(带注释)
    Object 对象有哪些方法?
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4745059.html
Copyright © 2011-2022 走看看