gpt4 book ai didi

java - 通过服务进行震动检测

转载 作者:行者123 更新时间:2023-11-29 20:49:21 24 4
gpt4 key购买 nike

我在创建抖动检测服务时遇到问题

后台服务

package com.likith.shakedetector;

import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.widget.Toast;

public class Background_service extends Service implements SensorEventListener
{
SensorManager sensorManager;
int count=0;

private float lastX = 0;
private float lastY = 0;
private float lastZ = 0;
private static final int MIN_FORCE = 10;
private static final int MIN_DIRECTION_CHANGE = 3;
private static final int MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE = 200;
private static final int MAX_TOTAL_DURATION_OF_SHAKE = 400;
private long mFirstDirectionChangeTime = 0;
private long mLastDirectionChangeTime;
private int mDirectionChangeCount = 0;

@Override
public IBinder onBind(Intent intent) {
return null;
}

public void onCreate()
{
super.onCreate();
}

public void onDestroy()
{
super.onDestroy();
sensorManager.unregisterListener(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_UI);

return START_STICKY;
}

public void onStart(Intent intent, int startId)
{
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_UI);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{

}

@SuppressWarnings("deprecation")
private void getAccelerometer(SensorEvent event)
{
//Toast.makeText(getApplicationContext(),"Shaked",Toast.LENGTH_LONG).show();

float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];

float totalMovement = Math.abs(x + y + z - lastX - lastY - lastZ);

if (totalMovement <= MIN_FORCE)
{
long now = System.currentTimeMillis();
if (mFirstDirectionChangeTime == 0)
{
mFirstDirectionChangeTime = now;
mLastDirectionChangeTime = now;
}

long lastChangeWasAgo = now - mLastDirectionChangeTime;

if (lastChangeWasAgo < MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE)
{
mLastDirectionChangeTime = now;
mDirectionChangeCount++;

lastX = x;
lastY = y;
lastZ = z;

if (mDirectionChangeCount >= MIN_DIRECTION_CHANGE)
{
long totalDuration = now - mFirstDirectionChangeTime;
if (totalDuration < MAX_TOTAL_DURATION_OF_SHAKE)
{
Toast.makeText(getApplicationContext(),"Shaked",Toast.LENGTH_LONG).show();
resetShakeParameters();
}
}

}
else
{
resetShakeParameters();
}
}
}


public void onSensorChanged(SensorEvent event)
{
getAccelerometer(event);
}

protected void onResume()
{
sensorManager.registerListener(this,sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_UI);
}

protected void onPause()
{
sensorManager.unregisterListener(this);
}

private void resetShakeParameters()
{
mFirstDirectionChangeTime = 0;
mDirectionChangeCount = 0;
mLastDirectionChangeTime = 0;
lastX = 0;
lastY = 0;
lastZ = 0;
}

当我运行此服务时,它会连续打印 toast 消息。

如何更改代码,以便在我摇动设备时,它只打印一次 toast 消息?

最佳答案

你有

float totalMovement = Math.abs(x + y + z - lastX - lastY - lastZ);

您可能应该分别处理每个轴 - 因为每个轴的变化独立于其他轴。

float xMovement = Math.abs(x - lastX);
float yMovement = Math.abs(y - lastY);
float zMovement = Math.abs(z - lastZ);

if ((xMovement > MIN_FORCE) ||
(yMovement > MIN_FORCE) ||
(zMovement > MIN_FORCE)) {
// motion detected
}

我在做类似的事情,发现最好先检查延迟 (MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE)。

Android API 18 有一个重要的运动传感器,如果存在于 Android 设备中可能对您的目的有效。

参见 http://developer.android.com/reference/android/hardware/Sensor.html#TYPE_SIGNIFICANT_MOTION

关于java - 通过服务进行震动检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com