例如。我正在使用加速度计,并且我想在另一个类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;
}
并将所有这些字段设为私有。