有人能告诉我当手机掉下来时我如何吐司吗? [英] Can anyone tell me how i get toast when mobile falls down?

查看:84
本文介绍了有人能告诉我当手机掉下来时我如何吐司吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个android项目来检测手机何时掉落,谁能告诉我在我的应用中应该使用哪个传感器,我知道加速度计将用于这种目的,但是加速度计还可以检测到我何时摇晃手机我只想在手机跌落时举杯. 这是我的代码:

I am making an android project to detect when mobile fall down, can anyone tell me which sensor should I use in my app, I know accelerometer will use for this kind of purpose, but accelerometer can also detect when I shake the phone in my hand and I want to get the toast only when the mobile falls down. here is my code:

int count = 1;
    private boolean init;
    private Sensor mySensor;
    private SensorManager SM;
    private float x1, x2, x3;
    private static final float ERROR = (float) 7.0;
    private static final float SHAKE_THRESHOLD = 15.00f; // m/S**2
    private static final int MIN_TIME_BETWEEN_SHAKES_MILLISECS = 1000;
    private long mLastShakeTime;
       public void onSensorChanged(SensorEvent event) {

            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

                long curTime = System.currentTimeMillis();
                if ((curTime - mLastShakeTime) > MIN_TIME_BETWEEN_SHAKES_MILLISECS) {

                    float x = event.values[0];
                    float y = event.values[1];
                    float z = event.values[2];

                    double acceleration = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)
                            + Math.pow(z, 2))
                            - SensorManager.GRAVITY_EARTH;
                    Log.d("mySensor", "Acceleration is " + acceleration + "m/s^2");

                    if (acceleration > SHAKE_THRESHOLD) {
                        mLastShakeTime = curTime;
                        Toast.makeText(getApplicationContext(), "FALL DETECTED",
                                Toast.LENGTH_LONG).show();
    } } }}

推荐答案

您好,我自己做了一点更改,使我摆脱了这个麻烦,那就是将两个变量设置为负值并将它们称为阈值加速的. 谢谢大家

Hi all i done this by myself a little change was get me rid of this , that is just taking two variable set those values negative and call them as threshold value of acceleration . thanks everyone

这篇关于有人能告诉我当手机掉下来时我如何吐司吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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