Android传感器值将在另一个函数中使用

时间:2014-11-29 20:33:13

标签: android eclipse orientation accelerometer sensor

在下面的代码中,我希望aX,oX等值用于另一个类的另一个函数。在这种情况下,函数来自matrixFunction。但是,传感器值的范围仅在while语句中。如何在变量中提取传感器值以用于另一个函数?在我的情况下matX.aEx等谢谢,

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub



     double oX = 0.0;       
     double oY = 0.0;
     double oZ = 0.0;


    while (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
    {
         oX = event.values[0];      
         oY = event.values[1];
         oZ = event.values[2];
         double timestamp_o = event.timestamp;

         String orientX = String.valueOf(oX);
         String orientY = String.valueOf(oY);
         String orientZ = String.valueOf(oZ);
         String timestamp_oS = String.valueOf(timestamp_o);

     {orientationX.setText("X: "+orientX +"\nY: "+orientY 
             + "\nZ: "+orientZ + "\nTimestamp: "+timestamp_oS);}

    }


    double aX = 0.0;        
    double aY = 0.0;        
    double aZ = 0.0;
    double timestamp_a = 0.0;

    while (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION)
    {
          aX = event.values[0];     
          aY = event.values[1];
          aZ = event.values[2];
          timestamp_a = event.timestamp;

         String acX = String.valueOf(aX);
         String acY = String.valueOf(aY);
         String acZ = String.valueOf(aZ);
         String timestamp_aS = String.valueOf(timestamp_a);

     { acceleration.setText("X: "+acX +"\nY: "+acY 
             + "\nZ: "+acZ + "\nTimestamp: "+timestamp_aS);}

    }

    double pressure_mbars = 0.0;

    while (event.sensor.getType() == Sensor.TYPE_PRESSURE)
    {
        pressure_mbars = event.values[0];
        double timestamp_p = event.timestamp;
        String timestamp_pS = String.valueOf(timestamp_p);

         // This is pressure in millibar. 


         String pressure_s = String.valueOf(pressure_mbars);
         pressureT.setText("Pressure: "+pressure_s +"\n"+timestamp_pS);



    }

    matrixFunction matX = new matrixFunction();

    double aEx = matX.aEx(aX, aY, aZ, oX, oY, oZ);
    double aEy = matX.aEy(aX, aY, aZ, oX, oY, oZ);
    double aEz = matX.aEz(aX, aY, aZ, oX, oY, oZ);

    String aEx_s = String.valueOf(aEx);
    String aEy_s = String.valueOf(aEy);
    String aEz_s = String.valueOf(aEz);


    accelerationInE.setText(aEx_s+"\n"+aEy+"\n"+aEz);

}

1 个答案:

答案 0 :(得分:0)

最简单的选择是在类中创建变量,并在onSensorChanged()中将它们设置为传感器读数。如果您无权访问该实例并且不想传递该实例,则还可以将变量设置为静态。