如何在Android应用程序中计算手机的振动

发布于 2024-11-03 17:06:26 字数 56 浏览 0 评论 0原文

在我的应用程序中,当用户行走时,我想计算手机的晃动(振动)。如何计算手机的晃动以及如何显示其输出。

in my app when the user walks i want to calculate the shaking(vibration) of a mobile. How to calculate the shaking of a mobile and how to show its output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂ガ小子 2024-11-10 17:06:26

这是使手机在摇动时振动的代码......

final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);        
 mShaker = new ShakeListener(this);        
 mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () 
 {
   public void onShake()
   {
      vibe.vibrate(100);
      System.out.println("SHAKE LISTENER CALLED");
      noofShakes++;
   }
  });



 public class ShakeListener implements SensorEventListener
{
      private static final int FORCE_THRESHOLD = 300;
      private static final int TIME_THRESHOLD = 100;
      private static final int SHAKE_TIMEOUT = 500;
      private static final int SHAKE_DURATION = 1000;
      private static final int SHAKE_COUNT = 3;

      private SensorManager mSensorMgr;
      private float mLastX=-1.0f, mLastY=-1.0f, mLastZ=-1.0f;
      private long mLastTime;
      private OnShakeListener mShakeListener;
      private Context mContext;
      private int mShakeCount = 0;
      private long mLastShake;
      private long mLastForce;

      public interface OnShakeListener
      {
        public void onShake();
      }

      public ShakeListener(Context context)
      {
        mContext = context;
        resume();
      }

      public void setOnShakeListener(OnShakeListener listener)
      {
        mShakeListener = listener;
      }

      public void resume() 
      {
        mSensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
        if (mSensorMgr == null) 
        {
          throw new UnsupportedOperationException("Sensors not supported");
        }

        boolean supported = false;

        try 
        {
            supported = mSensorMgr.registerListener(this, mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
        } 
        catch (Exception e) 
        {
            Toast.makeText(mContext, "Shaking not supported", Toast.LENGTH_LONG).show();
        }

        if ((!supported)&&(mSensorMgr != null)) mSensorMgr.unregisterListener(this);
      }

      public void pause() 
      {
        if (mSensorMgr != null) 
        {
          mSensorMgr.unregisterListener(this);
          mSensorMgr = null;
        }
      }

      public void onAccuracyChanged(Sensor sensor, int accuracy) 
      {

      }

      public void onSensorChanged(SensorEvent event)
      {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;
        long now = System.currentTimeMillis();

        if ((now - mLastForce) > SHAKE_TIMEOUT) 
        {
          mShakeCount = 0;
        }

        if ((now - mLastTime) > TIME_THRESHOLD) 
        {
          long diff = now - mLastTime;
          float speed = Math.abs(event.values[SensorManager.DATA_X] + event.values[SensorManager.DATA_Y] + event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
          System.out.println("SPEED OF THE VIBRATION "+speed);
          if (speed > FORCE_THRESHOLD) 
          {
            if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) 
            {
              mLastShake = now;
              mShakeCount = 0;
              if (mShakeListener != null) 
              {
                mShakeListener.onShake();
              }
            }
            mLastForce = now;
          }
          mLastTime = now;
          mLastX = event.values[SensorManager.DATA_X];
          mLastY = event.values[SensorManager.DATA_Y];
          mLastZ = event.values[SensorManager.DATA_Z];
        }
      }
}

我们必须在清单文件中添加以下行

 <uses-permission android:name="android.permission.SHAKE" />
    <uses-permission android:name="android.permission.VIBRATE" />

here is the code for making a mobile to get vibrated when is shaked....

final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);        
 mShaker = new ShakeListener(this);        
 mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () 
 {
   public void onShake()
   {
      vibe.vibrate(100);
      System.out.println("SHAKE LISTENER CALLED");
      noofShakes++;
   }
  });



 public class ShakeListener implements SensorEventListener
{
      private static final int FORCE_THRESHOLD = 300;
      private static final int TIME_THRESHOLD = 100;
      private static final int SHAKE_TIMEOUT = 500;
      private static final int SHAKE_DURATION = 1000;
      private static final int SHAKE_COUNT = 3;

      private SensorManager mSensorMgr;
      private float mLastX=-1.0f, mLastY=-1.0f, mLastZ=-1.0f;
      private long mLastTime;
      private OnShakeListener mShakeListener;
      private Context mContext;
      private int mShakeCount = 0;
      private long mLastShake;
      private long mLastForce;

      public interface OnShakeListener
      {
        public void onShake();
      }

      public ShakeListener(Context context)
      {
        mContext = context;
        resume();
      }

      public void setOnShakeListener(OnShakeListener listener)
      {
        mShakeListener = listener;
      }

      public void resume() 
      {
        mSensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
        if (mSensorMgr == null) 
        {
          throw new UnsupportedOperationException("Sensors not supported");
        }

        boolean supported = false;

        try 
        {
            supported = mSensorMgr.registerListener(this, mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
        } 
        catch (Exception e) 
        {
            Toast.makeText(mContext, "Shaking not supported", Toast.LENGTH_LONG).show();
        }

        if ((!supported)&&(mSensorMgr != null)) mSensorMgr.unregisterListener(this);
      }

      public void pause() 
      {
        if (mSensorMgr != null) 
        {
          mSensorMgr.unregisterListener(this);
          mSensorMgr = null;
        }
      }

      public void onAccuracyChanged(Sensor sensor, int accuracy) 
      {

      }

      public void onSensorChanged(SensorEvent event)
      {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;
        long now = System.currentTimeMillis();

        if ((now - mLastForce) > SHAKE_TIMEOUT) 
        {
          mShakeCount = 0;
        }

        if ((now - mLastTime) > TIME_THRESHOLD) 
        {
          long diff = now - mLastTime;
          float speed = Math.abs(event.values[SensorManager.DATA_X] + event.values[SensorManager.DATA_Y] + event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
          System.out.println("SPEED OF THE VIBRATION "+speed);
          if (speed > FORCE_THRESHOLD) 
          {
            if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) 
            {
              mLastShake = now;
              mShakeCount = 0;
              if (mShakeListener != null) 
              {
                mShakeListener.onShake();
              }
            }
            mLastForce = now;
          }
          mLastTime = now;
          mLastX = event.values[SensorManager.DATA_X];
          mLastY = event.values[SensorManager.DATA_Y];
          mLastZ = event.values[SensorManager.DATA_Z];
        }
      }
}

we have to add the following line in the manifest file

 <uses-permission android:name="android.permission.SHAKE" />
    <uses-permission android:name="android.permission.VIBRATE" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文