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

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

问题描述

我需要添加一个可以刷新我的 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.

有人对我如何创建这个 shake controller 有很好的指导吗?

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.

基于评论 - 对此进行测试:

Based on the comments - to test this:

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天全站免登陆