8

我正在实现一个演示定时器,带有振动(在特定条件下),当我按下开始时,我的定时器开始运行..当我使用停止按钮停止它时,它只是停止。

现在我必须集成一个功能,当人们移动设备时(在计时器运行时),它应该重置计时器。它工作得很好,但加速度计功能并不是绝对准确的。它需要一个快速的混蛋来重置计时器。

建议我一个很好的解决方案。

这是我的代码

public class SensorAccelerometer implements SensorEventListener {

    private Context context;
    private SensorManager sensorManager;
    private Sensor accelerometer;
    private TextView timelabel;
    private Handler mHandler;
    Runnable run;

    private float mLastX, mLastY, mLastZ;
    private final float NOISE = (float) 3.0;

    public SensorAccelerometer(Context context) {

    }


    public SensorAccelerometer(Context context,TextView timelabel, Handler mHandler2, Runnable mUpdateTimeTask) {
        // TODO Auto-generated constructor stub

        this.context = context;
        this.timelabel = timelabel;
        this.mHandler = mHandler2;
        this.run = mUpdateTimeTask;

        initialiseSensor();
    }


    public void initialiseSensor(){
        sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ALL);
        sensorManager.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void unregisterSensor(){
        sensorManager.unregisterListener(this);
        Toast.makeText(context, "Sensor Stopped..", Toast.LENGTH_SHORT).show();
    }


    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

    mAccelLast=mAccelCurrent;

    mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
    float delta = mAccelCurrent - mAccelLast;
    mAccel = mAccel * 0.9f + delta;

    if(mAccel>0.5){
        TimerActivity.mStartTime = SystemClock.uptimeMillis();
        mHandler.removeCallbacks(run);
        mHandler.postDelayed(run, 100);
    }

}

定时器活动

public class TimerActivity extends Activity {

    public static long mStartTime = 0L;
    private TextView mTimerLabel;

    private Handler mHandler = new Handler();

    String timerStop1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTimerLabel = (TextView) findViewById(R.id.textTimer);

        Button timerStartButton = (Button) findViewById(R.id.btnTimer);       
        timerStartButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){

                if(mStartTime == 0L){
                    mStartTime = SystemClock.uptimeMillis();
                    mHandler.removeCallbacks(mUpdateTimeTask);
                    mHandler.postDelayed(mUpdateTimeTask, 100);

                    //activating the sensor and the acclerometer
                    SensorAccelerometer acc = new SensorAccelerometer(view.getContext(), mTimerLabel,mHandler,mUpdateTimeTask);
                }                                   
            }
        }); 

        Button timerStopButton = (Button) findViewById(R.id.btnTimerStop);       
        timerStopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){

                mHandler.removeCallbacks(mUpdateTimeTask);
                mTimerLabel.setText(timerStop1);
                mStartTime = 0L;

                SensorAccelerometer scc = new SensorAccelerometer(view.getContext(),mTimerLabel,mHandler,mUpdateTimeTask);
                scc.unregisterSensor();
            }
        }); 

    } 


    private Runnable mUpdateTimeTask = new Runnable(){

        public void run() {

            final long start = mStartTime;
            long millis = SystemClock.uptimeMillis()- start;

            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            mTimerLabel.setText("" + minutes + ":"
                                  + String.format("%02d", seconds));                    

            timerStop1 = minutes + ":"
                      + String.format("%02d", seconds);

            mHandler.postDelayed(this, 200);            

        }   
    }; 

    protected void onPause() {
        super.onPause();
        SensorAccelerometer scc = new SensorAccelerometer(this,mTimerLabel,mHandler,mUpdateTimeTask);
        scc.unregisterSensor();
    };

} 
4

3 回答 3

6

我认为开发应用程序的下一个阶段是查看电子表格中生成的加速度值。我为此使用 Excel,但任何可以生成图表的工具都可以。所以改成onSensorChanged()类似的东西

public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

    float mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
    float mAccel = mAccel * 0.9f + mAccelCurrent * 0.1f;
    Log.d("onSensorChanged",System.currentTimeMillis()+","+mAccelCurrent +","+mAccel);

}

然后您可以将 currentTime、mAccelCurrent 和 mAccel 捕获到 Android 日志记录机制中。或者,创建您自己的文本文件,在其中写入值,然后在可以生成图形的工具中打开该文件。然后,您可以从图表中决定要用于触发器的值。

于 2013-05-16T07:59:40.813 回答
2

两个建议,一个想法:

  1. 对于一致的行为,您应该查看 event.values 向量的长度,即Math.sqrt(x*x+y*y+z*z),而不是单个值。从数学上讲,它Math.sqrt(x*x+y*y+z*z)与您的坐标系无关,即设备相对于地面的方向等,而单个数字 x、y、z 则不是。
  2. 您当前的应用程序会寻找加速度的变化。相反,我认为你应该只寻找高加速度,即Math.sqrt(x*x+y*y+z*z).
  3. 我最近写了这个关于将加速度计用于计步器的答案,我想你可能会觉得它很有趣。

于 2013-05-15T17:50:30.870 回答
1

要查找特定方向的线性加速度,您也可以使用以下代码:

    @Override
public void onSensorChanged(SensorEvent event) {
// alpha is calculated as t / (t + dT)
// with t, the low-pass filter's time-constant
// and dT, the event delivery rate

final float alpha = 0.8f;

gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
}  

计算线性加速度后,您可以简单地使用“if”语句来检查您的混蛋是否强大![更高的价值=更强大的混蛋]

于 2018-04-04T17:22:09.810 回答