安卓:我想动摇它我想

2023-09-11 10:43:15 作者:旧人旧城旧余温——

我需要添加一摇功能,将刷新我的Andr​​oid应用程序。

I need to add a shake feature that will refresh my Android application.

我所发现的文件涉及实施SensorListener,但是Eclipse告诉pcated我它的德$ P $和建议SensorEventListener。

All I find of documentation involves implementing the SensorListener, but Eclipse tells me it's deprecated and suggest SensorEventListener.

有人说有一个很好的指导我如何去建立这个的震动器的?

Anybody that has a nice guide to how I go about creating this shake controller?

推荐答案

下面是一个例子code。 将其放入您的活动类:

Here is an example code. Put this into your activity class:

  /* put this into your activity class */
  private SensorManager mSensorManager;
  private float mAccel; // acceleration apart from gravity
  private float mAccelCurrent; // current acceleration including gravity
  private float mAccelLast; // last acceleration including gravity

  private final SensorEventListener mSensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent se) {
      float x = se.values[0];
      float y = se.values[1];
      float z = se.values[2];
      mAccelLast = mAccelCurrent;
      mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
      float delta = mAccelCurrent - mAccelLast;
      mAccel = mAccel * 0.9f + delta; // perform low-cut filter
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
  };

  @Override
  protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    mSensorManager.unregisterListener(mSensorListener);
    super.onPause();
  }

和添加到您的onCreate方法:

And add this to your onCreate method:

    /* do this in onCreate */
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    mAccel = 0.00f;
    mAccelCurrent = SensorManager.GRAVITY_EARTH;
    mAccelLast = SensorManager.GRAVITY_EARTH;

然后你可以问mAccel无论你想在你的应用程序对于当前的加速,独立于轴,从静态加速度清洁如重力。 这将是约。 0如果没有运动,并且,可以说,> 2,如果设备被摇动

You can then ask "mAccel" wherever you want in your application for the current acceleration, independent from the axis and cleaned from static acceleration such as gravity. It will be approx. 0 if there is no movement, and, lets say >2 if the device is shaked.

根据的评论 - 测试这样的:

Based on the comments - to test this:

if (mAccel > 12) {
    Toast toast = Toast.makeText(getApplicationContext(), "Device has shaken.", Toast.LENGTH_LONG);
    toast.show();
}

注:

该accelometer应该被禁用的的onPause 的和激活的 onResume 的节约资源(CPU,电池)。 在code假设我们在地球上;-)并初始化加速地球重力。否则,你会得到一个强大的换血应用程序启动时,达到从自由落体的地面上。然而,code获取用于引力由于低切滤波器和也将工作在其他行星或在自由空间中,一旦被初始化。 (你永远不知道你的应用程序需要多长时间被使用......; - )

The accelometer should be deactivated onPause and activated onResume to save resources (CPU, Battery). The code assumes we are on planet Earth ;-) and initializes the acceleration to earth gravity. Otherwise you would get a strong "shake" when the application starts and "hits" the ground from free-fall. However, the code gets used to the gravitation due to the low-cut filter and would work also on other planets or in free space, once it is initialized. (you never know how long your application will be in use...;-)

 
精彩推荐
图片推荐