gpt4 book ai didi

java - 如何从 onSensorChanged 获取加速度计值到 ThreatForLoopA 类?

转载 作者:行者123 更新时间:2023-11-30 11:49:28 25 4
gpt4 key购买 nike

例如。我正在使用加速度计,我想要另一个类 ThreatForLoopA 中的值 x、y、z。

@Override
public void onSensorChanged(int sensor, float[] values) {
// TODO Auto-generated method stub

x = values[0];
y = values[1];
z = values[2];

ThreadForLoopA threadA = new ThreadForLoopA();

if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
long curTime = System.currentTimeMillis();

threadA.start();

这是 ThreatForLoopA 类的示例:

public class ThreadForLoopA extends Thread{

TextView xacc = null;
TextView yacc = null;
TextView zacc = null;

float[][] p= new float[5][3];
private long lastUpdate = -1;
public float x,y,z;
int row = 0;

@Override
public void run() {
// TODO Auto-generated method stub

long curTime = System.currentTimeMillis();

if (lastUpdate == -1 || (curTime - lastUpdate) > 2000) {
lastUpdate = curTime;

for (int column = 0; column < 3; column++) {
if (column == 0) {
p[row][column] = x;
//xacc.setText("Os X: " + p[row][column]);
}
if (column == 1) {
p[row][column] = y;
//yacc.setText("Os X: " + p[row][column]);
}
if (column == 2) {
p[row][column] = z;
//zacc.setText("Os X: " + p[row][column]);
}}
if (row == 0) {
xacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
}

如何从 onSensorChanged 获取加速度计值到 ThreatForLoopA 类?

最佳答案

好吧,将它们传递给构造函数:

ThreadForLoopA threadA = new ThreadForLoopA(x, y, z);

...

public ThreadForLoopA(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}

并将所有这些字段设为私有(private)。

关于java - 如何从 onSensorChanged 获取加速度计值到 ThreatForLoopA 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374591/

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