Android 如何检测安卓手机是否落地?

Android 如何检测安卓手机是否落地?,android,android-sensors,android-gravity,Android,Android Sensors,Android Gravity,我可以用传感器的加速度计算速度吗?哪种类型是传感器\类型\加速计? 或者我可以从传感器得到加速度的方向,哪种类型是传感器型加速度计? 我看过一些关于检查加速度向量值以检测android手机是否正在充电的博客。 但是,原因可能是安卓手机正在震动,无法填充。 我的代码是: /** * how often analysising the accelerometer?(Millisecond) */ private static final int ANALYSIS_ACCELERATION_INT

我可以用传感器的加速度计算速度吗?哪种类型是传感器\类型\加速计? 或者我可以从传感器得到加速度的方向,哪种类型是传感器型加速度计? 我看过一些关于检查加速度向量值以检测android手机是否正在充电的博客。 但是,原因可能是安卓手机正在震动,无法填充。 我的代码是:

/**
 * how often analysising the accelerometer?(Millisecond)
 */
private static final int ANALYSIS_ACCELERATION_INTERVAL = 500;
/**
 * how often checking the accelerometer?(Millisecond)
 */
private static final int CHECK_TIME_INTERVAL = 5000;
/**
 * when the sMinorMaxAccelerationInOneSecoud[0] is less than 
 * ACCELERATION_CRITICAL_VALUE[0],and the
 * sMinorMaxAccelerationInOneSecoud[1] is more than 
 * ACCELERATION_CRITICAL_VALUE[1],i think the phone dropped to the 
 * ground.
 */
private static float ACCELERATION_CRITICAL_VALUE[] = {2, 40};

/**
 * during{@link #ANALYSIS_ACCELERATION_INTERVAL},the min accelerometer  
 *  of the phone is sMinorMaxAccelerationInOneSecoud[0],the max 
 * accelerometer of the phone is saved at 
 * sMinorMaxAccelerationInOneSecoud[1].
 */
private static float sMinorMaxAccelerationInOneSecoud[] = {0, 0};

/**
 * the last time when checking the accelerometer
 */
private static long sLastCheckTime = 0;

/**
 * the last time when analysising the accelerometer
 */
private static long sLastAnalysisTime = 0;

/**
 * during {@link #CHECK_TIME_INTERVAL},is phone dropped to ground?
 */
private static boolean sIsFallDown = false;


private SensorManager mSensorManager;

public void receiveCondition() {
    // a dialog
    Intent intent = new Intent();
    intent.setClass(GlobalHolder.getApplicationContext(), PhoneDroppedDialog.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    GlobalHolder.getApplicationContext().startActivity(intent);
}

public void start() {
    mSensorManager = (SensorManager) getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(this,
            mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
            SensorManager.SENSOR_DELAY_UI);
}

public void stop() {
    mSensorManager.unregisterListener(this);
    mSensorManager = null;
    sMinorMaxAccelerationInOneSecoud = new float[]{0, 0};
    sLastCheckTime = 0;
    sLastAnalysisTime = 0;
}

@Override
public void onSensorChanged(SensorEvent event) {
    int sensorType = event.sensor.getType();
    if (sensorType != Sensor.TYPE_LINEAR_ACCELERATION) {
        return;
    }
    // judge if the sMinorMaxAccelerationInOneSecoud[0] is less than 
    // ACCELERATION_CRITICAL_VALUE[0],and the
    // sMinorMaxAccelerationInOneSecoud[1] is more than 
    // ACCELERATION_CRITICAL_VALUE[1]
    analysisAcceleration(event.values);

    long currentTime = System.currentTimeMillis();
    long beta = currentTime - sLastCheckTime;
    if (Math.abs(beta) < CHECK_TIME_INTERVAL) {
        return;
    }
    // it have been more than one secound since the last check,so we need re-check if the phone have dropped to the ground.
    checkIsFallDown();

    // reset sLastCheckTime with the current time
    sLastCheckTime = currentTime;

}

private void analysisAcceleration(float[] values) {
    if (sIsFallDown) {
        // if the phone have already dropped to the ground,we return directly.
        return;
    }

    // whether the min sum vector of the accelerations on the x_axis,y_axis,zaxis is less than ACCELERATION_CRITICAL_VALUE[0]
    // and the max sum vector of the accelerations on the x_axis,y_axis,zaxis is more than ACCELERATION_CRITICAL_VALUE[1]
    if (sLastAnalysisTime == 0) {
        sLastAnalysisTime = System.currentTimeMillis();
    }

    // the sum vector of the accelerations on the x_axis,y_axis,zaxis
    float currentAcceleration = 0f;
    for (int i = 0; i < values.length; i++) {
        currentAcceleration += (values[i] * values[i]);
    }
    currentAcceleration = (float) Math.sqrt(currentAcceleration);

    if (sMinorMaxAccelerationInOneSecoud[0] == 0 || sMinorMaxAccelerationInOneSecoud[0] > currentAcceleration) {
        sMinorMaxAccelerationInOneSecoud[0] = currentAcceleration;
    }

    if (sMinorMaxAccelerationInOneSecoud[1] == 0 || sMinorMaxAccelerationInOneSecoud[1] < currentAcceleration) {
        sMinorMaxAccelerationInOneSecoud[1] = currentAcceleration;
    }

    long currentTime = System.currentTimeMillis();
    long delta = currentTime - sLastAnalysisTime;
    if (delta < ANALYSIS_ACCELERATION_INTERVAL) {
        return;
    }
    MLog.d(TAG, "during ANALYSIS_ACCELERATION_INTERVAL, the min value is " + sMinorMaxAccelerationInOneSecoud[0] + ", the max value is " + sMinorMaxAccelerationInOneSecoud[1]);
    boolean isLessPre = sMinorMaxAccelerationInOneSecoud[0] < ACCELERATION_CRITICAL_VALUE[0];
    boolean isMoreNext = sMinorMaxAccelerationInOneSecoud[1] > ACCELERATION_CRITICAL_VALUE[1];
    sIsFallDown = isLessPre && isMoreNext;
    sLastAnalysisTime = currentTime;
    sMinorMaxAccelerationInOneSecoud = new float[]{0, 0};
}

private void checkIsFallDown() {
    if (sIsFallDown) {
        // the phone have dropped
        receiveCondition();
        MLog.d(TAG, "call the receiveCondition method.");
        sMinorMaxAccelerationInOneSecoud = new float[]{0, 0};
        sIsFallDown = false;
    }
}
/**
*多久分析一次加速计?(毫秒)
*/
专用静态最终整数分析\加速\间隔=500;
/**
*多久检查一次加速计?(毫秒)
*/
专用静态最终整数检查时间间隔=5000;
/**
*当SMInormaxAccelerationOneSecoud[0]小于
*加速度_临界值[0],以及
*SminorMaxAccelerationOneSecoud[1]大于
*加速度临界值[1],我想手机掉到了
*地面。
*/
私有静态浮点加速度_临界值[]={2,40};
/**
*在{@link#分析_加速度_间隔}期间,最小加速度计
*手机的最大容量为SminorMaxAccelerationOneSecoud[0]
*手机的加速计储存在
*SminorMaxionOneSecoud[1]。
*/
私有静态浮点sminoMaxAccelerationOneSecoud[]={0,0};
/**
*最后一次检查加速计时
*/
私有静态长sLastCheckTime=0;
/**
*最后一次分析加速度计时
*/
私有静态长slatanalysistime=0;
/**
*在{@link#CHECK _TIME _INTERVAL}期间,手机是否掉到地上?
*/
私有静态布尔值sIsFallDown=false;
私人传感器管理器;
公共无效接收条件(){
//对话
意图=新意图();
setClass(GlobalHolder.getApplicationContext(),PhoneDroppedDialog.class);
intent.setFlags(intent.FLAG\u活动\u新任务);
GlobalHolder.getApplicationContext().startActivity(intent);
}
公开作废开始(){
MSSensorManager=(SensorManager)getApplicationContext().getSystemService(Context.SENSOR\u服务);
mSensorManager.registerListener(此,
msSensorManager.getDefaultSensor(传感器类型为线性加速度),
SensorManager.SENSOR\u DELAY\u UI);
}
公共停车场(){
mSensorManager.unregisterListener(此);
mSensorManager=null;
sminoMaxAccelerationOneSecoud=新浮点[]{0,0};
sLastCheckTime=0;
sLastAnalysisTime=0;
}
@凌驾
传感器更改时的公共无效(传感器事件){
int sensorType=event.sensor.getType();
if(传感器类型!=传感器类型\线性\加速度){
返回;
}
//判断SMInormaxAccelerationOneSecoud[0]是否小于
//加速度_临界值[0],以及
//SminorMaxAccelerationOneSecoud[1]大于
//加速度临界值[1]
分析加速度(事件值);
长currentTime=System.currentTimeMillis();
长beta=当前时间-sLastCheckTime;
if(数学绝对值(β)<检查时间间隔){
返回;
}
//自从上次检查以来已经超过一秒了,所以我们需要重新检查手机是否掉到地上。
checkIsFallDown();
//用当前时间重置sLastCheckTime
sLastCheckTime=当前时间;
}
私有void分析加速(浮点[]值){
如果(下拉列表){
//如果电话已经掉到地上,我们直接返回。
返回;
}
//x_轴、y_轴、zaxis上加速度的最小和向量是否小于加速度_临界值[0]
//x_轴,y_轴,zaxis上加速度的最大和向量大于加速度的临界值[1]
如果(sLastAnalysisTime==0){
slatanalysistime=System.currentTimeMillis();
}
//x_轴、y_轴、zaxis上加速度的和向量
浮子电流加速度=0f;
对于(int i=0;icurrentAcceleration){
SminorMaxAccelerationOneSecoud[0]=当前加速度;
}
if(sminoMaxAccelerationOneSecoud[1]==0 | | sminoMaxAccelerationOneSecoud[1]加速度临界值[1];
sIsFallDown=isLessPre&isMoreNext;
slatanalysistime=当前时间;
sminoMaxAccelerationOneSecoud=新浮点[]{0,0};
}
私有void checkIsFallDown(){
如果(下拉列表){
//电话掉了
receiveCondition();
d(标记“调用receiveCondition方法”);
sminoMaxAccelerationOneSecoud=新浮点[]{0,0};
sIsFallDown=false;
}
}
有人有更好的主意吗?谢谢