摇动设备时如何刷新应用程序? [英] How to refresh app upon shaking the device?

查看:133
本文介绍了摇动设备时如何刷新应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加一个震动功能来刷新我的Android应用程序。

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

我发现的所有文档都涉及实现 SensorListener ,但是Eclipse告诉我它已被弃用,建议使用 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?

推荐答案

下面是示例代码。
将其放入您的活动类别:

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.

根据评论进行测试:

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

注意:

应禁用加速度计 onPause 并激活 onResume 以节省资源(CPU,电池)。
代码假定我们在地球;-)上,并将加速度初始化为地球重力。否则,当应用程序启动并从自由落体中撞到地面时,您将获得强烈的震动。但是,由于使用了低切滤波器,该代码已习惯了引力,并且一旦初始化,该代码也可以在其他行星或自由空间中工作。
(您永远不知道您的应用程序将使用多长时间...;-)

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...;-)

这篇关于摇动设备时如何刷新应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆